【问题标题】:Adding xml element to a list from XML reader从 XML 阅读器将 xml 元素添加到列表
【发布时间】:2013-02-02 09:48:08
【问题描述】:

我有一个名为投资组合的 xml 文件,我将其位置作为字符串传递。 从元素下的投资组合文件中读取文件名列表。在 xml 文件中,我有一个名为的元素,我需要读取价格数据中的 4 个值并将其存储到字符串列表中。我不知道我这样做是否正确。我不知道 foreach 循环的参数应该是什么。

XML 文件:

<priceData>
    <file name="ibmx.xml"/>
    <file name="msft.xml"/>
    <file name="ulti.xml"/>
    <file name="goog.xml"/>
</priceData>

这是我的 C# 函数

public static  void readPortfolio(string filename)
{
    XmlTextReader reader = new XmlTextReader(filename);
    reader.Read();
    List<string> priceDataFile = new List <string> ();
    foreach(var file in reader) //Don't know what the parameters should be.
    {
        priceDataFile.Add(reader.Value); //Not sure if I am passing what I want
    }
}

【问题讨论】:

    标签: c# .net xml list xmlreader


    【解决方案1】:

    你可以这样做。以下将每个属性的文件名添加到列表中 用您的路径位置替换我声明您的.XML 文件副本的位置。

    XDocument document = XDocument.Load(@"C:\Sample_Xml\PriceData.xml");
    List<string> priceDataFile = new List<string>();
    var priceData = (from pd in document.Descendants("priceData")
                      select pd);
    
    foreach (XElement priceValue in priceData.Elements())
    {
        priceDataFile.Add(priceValue.FirstAttribute.Value.ToString());
    }
    

    这就是您的 priseDataFile 列表内容的样子 在 QuickWatch 中查看

    [0] "ibmx.xml"
    [1] "msft.xml"
    [2] "ulti.xml"
    [3] "goog.xml"
    

    【讨论】:

    • 这里不需要ToList()
    • 我编辑了答案并对其进行了测试,感谢 abatishchev
    【解决方案2】:

    改用 LINQ to XML (.NET 3.0+):

    XDocument doc = XDocument.Load(path);
    List<string> list = doc.Root
                           .Elements("file")
                           .Select(f => (string)f.Atribute("name"))
                           .ToList();
    

    【讨论】:

      【解决方案3】:

      使用 XDocument 类是一个很好的解决方法。但是如果你想使用 XmlTextReader 类,代码已经列出如下。然后您将获得包含 XML 文件列表的结果。另一方面,名称是示例代码中的一个属性。所以你应该使用 reader.GetAttribute("name") 来获取价值。

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Xml;
      using System.Xml.Linq;
      
      namespace XmlReaderTest
      {
          class Program
          {
              static void Main(string[] args)
              {
                  XmlTextReader reader = new XmlTextReader("../../Portfolio.xml");
                  reader.WhitespaceHandling = WhitespaceHandling.None;
                  List<string> priceDataFile = new List<string>();
                  while (reader.Read())
                  {
                      if (reader.Name == "file")
                      {
                          priceDataFile.Add(reader.GetAttribute("name"));
                      }
                      else
                          continue;
                  }
      
                  reader.Close();
      
                  foreach (string file in priceDataFile)
                  {
                      Console.WriteLine(file);
                  }
      
                  Console.ReadLine();
              }
          }
      }
      

      【讨论】:

      • @DJ KRAZE 我同意你的看法。如果 Martin 没有必要使用 XmlTextReader 类,XDocument 类更好。 Linq 将简化您的代码。
      猜你喜欢
      • 2019-10-14
      • 2012-04-14
      • 2013-07-19
      • 2015-10-08
      • 2013-08-16
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多