Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从xml读取器向列表中添加xml元素_C#_.net_Xml_List_Xmlreader - Fatal编程技术网

C# 从xml读取器向列表中添加xml元素

C# 从xml读取器向列表中添加xml元素,c#,.net,xml,list,xmlreader,C#,.net,Xml,List,Xmlreader,我有一个名为portfolio的xml文件,我将其位置作为字符串传递。 从公文包文件中的元素下读取文件名列表。在xml文件中,我有一个名为的元素,我需要读取价格数据中的4个值,并将其存储到字符串列表中。我不知道我这样做是否正确。我不知道foreach循环的参数应该是什么 XML文件: <priceData> <file name="ibmx.xml"/> <file name="msft.xml"/> <file name="ul

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

XML文件:

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

这是我的C函数#

publicstaticvoidreadportfolio(字符串文件名)
{
XmlTextReader=新的XmlTextReader(文件名);
reader.Read();
List priceDataFile=新列表();
foreach(reader中的var文件)//不知道参数应该是什么。
{
priceDataFile.Add(reader.Value);//不确定是否传递了所需的内容
}
}

您可以这样做。下面将向列表中添加每个属性的文件名 将我声明的
.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());
}

使用XDocument类是解决这个问题的一个好方法。然后您将得到包含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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间XmlReaderTest
{
班级计划
{
静态void Main(字符串[]参数)
{
XmlTextReader=新的XmlTextReader(“../../Portfolio.xml”);
reader.WhitespaceHandling=WhitespaceHandling.None;
List priceDataFile=新列表();
while(reader.Read())
{
如果(reader.Name==“文件”)
{
Add(reader.GetAttribute(“name”);
}
其他的
继续;
}
reader.Close();
foreach(priceDataFile中的字符串文件)
{
Console.WriteLine(文件);
}
Console.ReadLine();
}
}
}
使用LINQ转换为XML(.NET 3.0+):

XDocument doc=XDocument.Load(路径);
List=doc.Root
.Elements(“文件”)
.选择(f=>(字符串)f.Atribute(“名称”))
.ToList();

@DJ KRAZE我同意你的看法。若马丁并没有必要使用XmlTextReader类,那个么XDocument类更好。Linq将简化您的代码。您不需要在此处使用
ToList()
。我编辑了答案并对其进行了测试。谢谢abatishchev
[0] "ibmx.xml"
[1] "msft.xml"
[2] "ulti.xml"
[3] "goog.xml"
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();
        }
    }
}
XDocument doc = XDocument.Load(path);
List<string> list = doc.Root
                       .Elements("file")
                       .Select(f => (string)f.Atribute("name"))
                       .ToList();