-3
<Peoples>
 <People>
  <Name>RadheyJang</Name> 
  <Location>India</Location> 
  <Work>Software Developer</Work> 
  <Point>5</Point> 
  <details>
    <People>
    <Name>ArunaTiwari</Name> 
    <Location>India</Location> 
    <Work>SoFtwareCoder</Work> 
    <Point>3</Point> 
    <details/>
    <Test>A</Test>
    </People>
  </details>
  <Test>NA</Test>    
 </People>
</Peoples>

我可以通过使用下面的代码来阅读那个 Xml。

                       XDocument xmlDoc = XDocument.Load(str);
                       var vrresult = from a in xmlDoc.Descendants("People") 
                       select new
                       {
                           Name= a.Element("Name").Value,
                           Location= a.Element("Location").Value,
                           Point= a.Element("Point").Value
                       };

                        GridView1.DataSource = vrresult;
                        GridView1.DataBind();

但它也在阅读详细的内容。我想跳过阅读details Element 中的内容。请让我知道如何跳过详细信息中的内容。

4

3 回答 3

1

您需要为此使用 XPath...

using System.Xml.XPath;

string xml = @"
    <Peoples>
        <People>
        <Name>RadheyJang</Name> 
        <Location>India</Location> 
        <Work>Software Developer</Work> 
        <Point>5</Point> 
        <details>
            <People>
            <Name>ArunaTiwari</Name> 
            <Location>India</Location> 
            <Work>SoFtwareCoder</Work> 
            <Point>3</Point> 
            <details/>
            <Test>A</Test>
            </People>
        </details>
        <Test>NA</Test>    
        </People>
    </Peoples>";

XDocument xmlDoc = XDocument.Parse(xml);

var vrresult = from a in xmlDoc.XPathSelectElements("/Peoples/People")
                select new
                {
                    Name = a.Element("Name").Value,
                    Location = a.Element("Location").Value,
                    Point = a.Element("Point").Value
                };
于 2012-05-21T07:42:15.137 回答
0

我唯一能想到的是 XML 是不合法的:

<Peoples>
 <People> *You have an opening People tag here*
  <Name>RadheyJang</Name> 
  <Location>India</Location> 
  <Work>Software Developer</Work> 
  <Point>5</Point> 
  <details> *You create a details tag here*
   <People> *You generate the same tag inside of another People tag*
    <Name>ArunaTiwari</Name> 
    <Location>India</Location> 
    <Work>SoFtwareCoder</Work> 
    <Point>3</Point> 
    <details/> *Then you close the details tag here*
    <Test>A</Test>
    </People>
  </details> *Then you close another one, but there is not a second opening detail tag*
  <Test>NA</Test>    
 </People>
</Peoples>

我不确定这是否有帮助,但您可能需要考虑修复您的 XML。

于 2012-05-21T05:39:26.023 回答
0
    var ele = XElement.Parse(xml);
    // change to XElement.Load if loading from file  
    var result = ele.Descendants("Section").Zip(ele.Descendannt("Mark"),  (s,m) => new {Section = s.Value, Mark = m.Value}); Now you can create your DataTable:

    var table = new DataTable(); 
    var marks = new DataColumn("Mark");
    var sections = new     DataColumn("Sections"); 
    table.Columns.Add(marks); table.Columns.Add(sections); 
    foreach (var item in result) 
    {   
     var row = table.NewRow();   
     row["Mark"] = item.Mark;     
     row["Sections"] = item.Section; 
     table.Rows.Add(row);
    } 

试试这个代码..

于 2012-05-21T04:59:18.407 回答