【问题标题】:Reading in XML in C# - Grab specific Element在 C# 中读取 XML - 获取特定元素
【发布时间】:2016-03-23 05:12:13
【问题描述】:

我正在阅读一些 XML 并将其显示在一个效果很好的 DataGridView 中。

但是,如果我能抓取特定元素并阅读它们的内容,我的生活会轻松 10 倍。 (我使用 XmlReader)

另外,有没有一种简单的方法来循环一个元素?我需要获取计数,然后遍历每个“设施”,但我似乎无法让它正常工作。添加另一个“While(read)”并不是一个很好的解决方案。

我现在不想切换到另一个阅读器,我现在对这个很深入。

XML 数据:

    <data>
    <count>2</count>
    <facility>
        <typeId>1</typeId>
        <type>Beds</type>
        <quantity>0</quantity>
        <description>null</description>
    </facility>
    <facility>
        <typeId>2</typeId>
        <type>EDBeds</type>
        <quantity>0</quantity>
        <description>null</description>
    </facility>
</data>

代码:

while (xr.Read())
{
    if (xr.Name.Equals("count"))
    {
        valueName.Text = "We currently have " + xr.ReadElementContentAsString() + " facilities open.";
    }
    while (xr.ReadToFollowing("facility"))
    {
        //Add a new row for data if we are at a new Facility element
        if (xr.NodeType == XmlNodeType.Element && xr.Name == "facility")
        {
            row = generalData.Rows.Add();
            col = 0;
        }
        //Loop through current facility Element
    }
}

【问题讨论】:

    标签: c# xml xmlreader


    【解决方案1】:

    跳出枪来,使用“ReadSubTree()”可以很好地解决这个问题:

                        XmlReader inner = xr.ReadSubtree();
                        while (inner.Read())
                        {
                            if (xr.NodeType == XmlNodeType.Text)
                            {
                                 //Do stuff
                            }
                        }
    

    【讨论】:

      【解决方案2】:

      看到这个post 为什么你应该更喜欢XDocument 而不是XmlReader。正如它所说,XDocument 的 API 要容易得多,并且支持“LINQ To XML”。 XmlReader 已经过时,因为它已经过时并且包含错​​误。

      var xml = @"
      <data>
          <count>2</count>
          <facility>
              <typeId>1</typeId>
              <type>Beds</type>
              <quantity>0</quantity>
              <description>null</description>
          </facility>
          <facility>
              <typeId>2</typeId>
              <type>EDBeds</type>
              <quantity>0</quantity>
              <description>null</description>
          </facility>
      </data>";
      
      var xDocument = XDocument.Load( new StringReader( xml ) );
      var facilityElements = xDocument.Descendants().Where( x => x.Name == "facility" );
      
      // Count the elements
      var count = facilityElements.Count();
      
      foreach ( var facilityElement in facilityElements )
      {
          // Handle elements
      }
      
      // Adds an facility element
      var facility = new XElement( "facility" );
      var typeId = new XElement( "typeId" );
      typeId.SetValue(3);
      facility.Add( typeId );
      xDocument.Element( "data" ).Add( facility );
      

      【讨论】:

      • 我完全同意,如果有人看到这个问题,我强烈建议您使用其他阅读器。 XmlReader 有点麻烦。
      【解决方案3】:

      对于给定的 XML 数据,我们不需要遍历每个 'facility' 元素来将其数据绑定到 DataGridView。

      相反,我们可以按照以下简单步骤,

      DataSet ds = new DataSet();
      ds.ReadXml(xr);
      dataGridView1.DataSource = ds.Tables["facility"];
      

      --SJ

      【讨论】:

      • 太棒了!这样就可以了
      猜你喜欢
      • 1970-01-01
      • 2018-12-05
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 2022-11-10
      相关资源
      最近更新 更多