0
Dataset ds=new DataSet();
ds.ReadXml("path of the xml");

However ds has multiple tables, I would like to fetch out the details tag information of Header and Footer separately:

XElement xElement = XElement.Parse(ds.GetXml().ToString());
  var items =xElement
                  .Descendants("Header");

Above code doesn't yield me the results, it comes back as empty.

How can i get Name & Number tag of Details tag per Header and per Footer? Can i create 2 datasets using ds.ReadXML separtely?

Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<Mapping>
 <Header>
   <Row>
      <Details>
      <Name>Name</Name> 
         <DataType>string</DataType>
         <Value>Mark</Value>
      </Details>
      <Details>
      <Name>Number</Name> 
         <DataType>int</DataType>
         <Value>1</Value>
      </Details>
   </Row>    
 </Header>
<Footer>
   <Row>
      <Details>
      <Name>Name</Name> 
         <DataType>string</DataType>
         <Value>John</Value>
      </Details>
      <Details>
      <Name>Number</Name> 
         <DataType>int</DataType>
         <Value>2</Value>
      </Details>
   </Row>
</Footer>
</Mapping>

Dataset 1 : Header info - So that i can loop thro' the rows

Dataset 2 : Footer info - So that i can loop thro' the rows

Or is there any other approach to fetch out name, number separately? the objective here is to fetch out the data and build a C# class like

public class Header
{
 public Header(){}

 public string Name;
 public int Number

}


public class Footer
{
 public Footer(){}

 public string Name;
 public int Number

}
4

1 回答 1

1
XElement mapping = XElement.Parse(ds.GetXml().ToString());
var query = from d in mapping.Descendants("Details")
            select new {
                Name = (string)d.Element("Name"),
                Number = (int)d.Element("Number")
            };

如果你想构建你的类实例:

var headers = from d in mapping.Element("Header")
                               .Element("Row").Elements("Details")
              select new Header {
                  Name = (string)d.Element("Name"),
                  Number = (int)d.Element("Number")
              };

获取页脚是相同的,除了你应该"Footer"先选择元素。


您还可以使用 XPath 来选择元素:

var headers = from d in mapping.XPathSelectElements("Header/Row/Details")
              select new Header {
                  Name = (string)d.Element("Name"),
                  Number = (int)d.Element("Number")
              };
于 2013-08-21T23:32:43.677 回答