【问题标题】:Read only parts of an XML file只读部分 XML 文件
【发布时间】:2012-05-27 14:44:13
【问题描述】:
<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 元素内的内容。请让我知道如何跳过详细信息中的内容。

【问题讨论】:

  • 我以前从未见过该错误消息...您能否编辑您的答案并添加 GridView 标记以及绑定您的 XML 的代码?
  • Gridview 绑定没有问题。我会在阅读时绑定但在绑定之前它给出错误。
  • 啊,明白了。我没有给你答案,但也许这个链接会有所帮助:dotnet247.com/247reference/msgs/5/26129.aspx
  • 像您在此处所做的那样更改问题的内容被认为是不好的做法。第一次编辑是您收到的错误消息。然后您将其更改为“如何阅读某些内容..”。下一次,打开一个新问题。
  • 请问为什么要跳过详细内容?无论如何,您都需要阅读它以了解详细信息标签的结束位置(以便阅读 xml 文件的其余部分)。您想在这里实现什么目标?

标签: c# asp.net xml linq


【解决方案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
                };

【讨论】:

    【解决方案2】:
        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);
        } 
    

    试试这个代码..

    【讨论】:

    • 我尝试使用此代码,但我想读取 3 个值,您只读取 2 个值。请让我知道如何读取 3 个值和跳过详细信息属性。
    【解决方案3】:

    我唯一能想到的就是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。

    【讨论】:

    • It it system Generated XMl File .
    • @rama 不过,如果您的某个应用程序正在创建它,那么您可能想要更改它的生成方式。
    • @MatthewRz 他没有用&lt;details/&gt; 关闭&lt;details&gt; 标签。那是一个空的&lt;details&gt; 标签。标签的关闭是用&lt;/details&gt; 完成的
    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 2021-03-24
    • 2022-01-08
    • 2014-11-23
    • 2017-04-13
    • 1970-01-01
    相关资源
    最近更新 更多