【问题标题】:xml, xmlreader read only specific part using c#xml, xmlreader 使用 c# 只读特定部分
【发布时间】:2017-04-13 01:51:11
【问题描述】:

我正在使用 c# 从 xml 文件中获取参数。我的问题是我只想读取当前程序参数。 (v1.0, v1.1, v1.2...)

<?xml version="1.0" encoding="utf-8" ?>
<ApplicationPool>
    <Resource Version="V1.0">
        <Input>2000</Input>
        <Input>210</Input>
        <Input>65000</Input>
    </Resource>
    <Resource Version="V1.1">
        <Input>2500</Input>
        <Input>400</Input>
        <Input>130000</Input>
    </Resource>
</ApplicationPool>

using (XmlReader reader = XmlReader.Create("testXml.xml"))
{
    while (reader.Read())
    {
        if (reader.IsStartElement())
        {
            if (reader["Version"] == actualVersion)
            {
                //??
            }
        }
    }
}

【问题讨论】:

  • 你有什么理由在这里使用XmlReader吗?如果可能,我会强烈建议您使用 LINQ to XML。我只会使用XmlReader 来读取潜在的巨大文件而不将其加载到内存中,或者为了实现XmlReader 中描述的接口。

标签: c# .net xml xmlreader


【解决方案1】:
XDocument doc = XDocument.Load("testXml.xml")

var result = doc.Root.Descendants("Resource")
                      .Where(x => x.Attribute("Version")!= null 
                                   && x.Attribute("Version").Value == actualVersion);

这将返回属性Version == actualVersion 所在的所有Resource 节点。之后你可以对节点做任何你想做的事情。

【讨论】:

    【解决方案2】:
    if (reader["Version"] == actualVersion)
    {
        while (reader.ReadToFollowing("Input"))
        {
            string value = reader.ReadElementContentAsString();
            // or
            int value = reader.ReadElementContentAsInt();
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以像这样使用 Xml Linq 方法:

      var xmlFile= XElement.Load(xmlString);
      var actualVersion = "V1.1";
      var requiredXmlData = xmlFile.Elements("Resource").Where(c=>c.Attribute("Version").Value==actualVersion );
      

      【讨论】:

        【解决方案4】:
        using System.Xml;
        ...
        string actualVersion="V1.1";
        XmlDocument rssDoc = new XmlDocument();
        rssDoc.Load("testXML.xml");
        XmlNodeList _ngroups = rssDoc.GetElementsByTagName("Resource");
        for(int i=0;i<=_ngroups.Count-1;i++)
        {
            if(_ngroups[i].Attributes[0].InnerText.ToString()==actualVersion)
            {
                for(int j=0;j<=_ngroups[i].ChildNodes.Count-1;j++)
                      Console.WriteLine(_ngroups[i].ChildNodes[j].InnerText);
           }
        }
        ...
        

        【讨论】:

          【解决方案5】:

          结合使用 XmlReader 和 xml linq

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Xml;
          using System.Xml.Linq;
          
          namespace ConsoleApplication1
          {
              class Program
              {
                  const string FILENAME = @"c:\temp\test.xml";
                  static void Main(string[] args)
                  {
                      XmlReader reader = XmlReader.Create(FILENAME);
          
                      while (!reader.EOF)
                      {
                          if (reader.Name != "Resource")
                          {
                              reader.ReadToFollowing("Resource");
                          }
                          if (!reader.EOF)
                          {
                              XElement resource = (XElement)XElement.ReadFrom(reader);
                              string version = (string)resource.Attribute("Version");
                          }
                      }
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-01-27
            • 1970-01-01
            • 1970-01-01
            • 2023-03-30
            • 1970-01-01
            • 2013-09-08
            • 2012-02-11
            • 2016-12-05
            相关资源
            最近更新 更多