locked
WebMatrix: Using XML data for WebGrid RRS feed

  • Question

  • User694103386 posted

     Hi All,

    right now I playing around with WebMatrix and I followed the tutorial from http://www.asp.net/WebMatrix. Chapter 6 (displaying data in a grid) described how to use  a WebGrid helper very clear, but the data in the example is always retrieved from an SQL DB.

    I want to use the same XML file from chapter 7 (Displaying data in a chart).

     

    Using the code:

    ...

    var DataSet = new DataSet();

    dataSet.ReadXmlSchema(Server.MapPath("/App_Data/QCFiler.xsd"));

    dataSet.ReadXml(Server.MapPath("/App_Data/QCFiler.xml"));

    var grid = new WebGrid(dataSet.Tables[0]);

    ...

     

    Brings the error:

    Can`t convert  "System.Data.DataTable" to "System.Collections.Generic.IEnumerable<dynamic>".

     

    So DataSet doesn`t seems to be the correct way and I`ve to convert the data before.  Any hints on this?

     

    Regards,

    Mikosch

     

     

     

     

     

     

     

    Thursday, February 3, 2011 5:57 PM

Answers

  • User-633481390 posted

    Linq to Xml would be the best way to go about this. Specifically for the sample given in Chapter 7

    <NewDataSet>
        <Employee>
            <Name>Erin</Name>
            <Sales>10440</Sales>
        </Employee>
    ....

    you would do 

    @using System.Xml.Linq
    
    @{
    var file = XDocument.Load(Server.MapPath(@"/App_Data/XmlFile.xml"));
    var results = from e in file.Root.Elements()
                        select new { Name = e.Element("Name").Value, Sales = e.Element("Sales").Value };
    var grid = new WebGrid(results);
    ...


    Note: I've removed the namespace attribute (xmlns) from the xml to make the example simpler




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 3, 2011 10:31 PM

All replies

  • User-633481390 posted

    Linq to Xml would be the best way to go about this. Specifically for the sample given in Chapter 7

    <NewDataSet>
        <Employee>
            <Name>Erin</Name>
            <Sales>10440</Sales>
        </Employee>
    ....

    you would do 

    @using System.Xml.Linq
    
    @{
    var file = XDocument.Load(Server.MapPath(@"/App_Data/XmlFile.xml"));
    var results = from e in file.Root.Elements()
                        select new { Name = e.Element("Name").Value, Sales = e.Element("Sales").Value };
    var grid = new WebGrid(results);
    ...


    Note: I've removed the namespace attribute (xmlns) from the xml to make the example simpler




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 3, 2011 10:31 PM
  • User694103386 posted

    Hi Pranavkm,

     

    thanks for the answer, that is working fine, but...

     

    Is there a way to use the XML including the XML Namespace, because I want to use the XML for a WebGrid and a WebChart..... 

     

    Regards,

    Mikosch

    Friday, February 4, 2011 5:43 AM
  • User-633481390 posted

    Sure, you'll need to use the expanded name to pick up elements (http://msdn.microsoft.com/en-us/library/system.xml.linq.xname.aspx) which would be of the format "{namespace}localName" or go about selecting elements by their index Name = e.Elements().ElementAt(0).Value

    The MSDN article at http://msdn.microsoft.com/en-us/library/bb387098.aspx would be a good start if you are unfamiliar with Linq to Xml.


    Friday, February 4, 2011 10:50 AM