C# XML文件的只读部分

C# XML文件的只读部分,c#,asp.net,xml,linq,C#,Asp.net,Xml,Linq,但它也在阅读细节的内容。我想跳过阅读详细信息元素中的内容。请让我知道如何跳过详细信息中的内容 XDocument xmlDoc = XDocument.Load(str); var vrresult = from a in xmlDoc.Descendants("People") select new {

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

                       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();

试试这段代码。

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

    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);
    } 

*这里有一个开始的人物标签*
拉德江
印度
软件开发人员
5.
*您可以在此处创建详细信息标记*
*在另一个人物标签中生成相同的标签*
阿鲁纳提瓦里
印度
软件编码器
3.
*然后在这里关闭详细信息标签*
A.
*然后关闭另一个,但没有第二个“打开详细信息”标记*
NA

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

你需要使用xPath来这个……/P>
<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>
使用System.Xml.XPath;
字符串xml=@“
拉德江
印度
软件开发人员
5.
阿鲁纳提瓦里
印度
软件编码器
3.
A.
NA
";
XDocument xmlDoc=XDocument.Parse(xml);
var vrresult=来自xmlDoc.XPathSelectElements(“/Peoples/People”)中的
选择新的
{
Name=a.Element(“Name”).Value,
位置=一个元素(“位置”)值,
点=一个元素(“点”).值
};

我以前从未看到过该错误消息。。。您能编辑您的答案并添加GridView标记和绑定XML的代码吗?GridView绑定没有问题。我将绑定,但在绑定之前,在阅读时,它会给出错误。啊,明白了。我没有答案给你,但也许这个链接会有所帮助:像你在这里做的那样改变问题的内容被认为是不好的做法。第一次编辑是您收到的错误消息。然后你把它改为“如何阅读某些内容…”。下次,打开一个新问题。我可以问你为什么要跳过详细内容吗?无论如何,您都需要阅读它才能知道details标记的结束位置(以便读取xml文件的其余部分)。你们想在这里实现什么?我试着用这段代码,但我想读3个值,你们只读2个值。请告诉我如何读取3个值并跳过“详细信息”属性。它是系统生成的XMl文件。@rama不过,如果您的某个应用程序正在创建它,您可能需要更改它的生成方式。@MatthewRz他没有用
关闭
标记。这是一个空的
标记。使用
<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>
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
                };