Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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/3/wix/2.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 c中获取所有值#_C#_Xml - Fatal编程技术网

C# 如何从同名xml c中获取所有值#

C# 如何从同名xml c中获取所有值#,c#,xml,C#,Xml,我的目的是制作一个程序,创建一个新的文件目标,从另一个文件中获取一些值。 XML文件: <?xml version="1.0"?> <house> <chairs count="3"> <chairs>a</chairs> <chairs>b</chairs> <chairs>c</chairs> </chairs> </house>

我的目的是制作一个程序,创建一个新的文件目标,从另一个文件中获取一些值。 XML文件:

 <?xml version="1.0"?>
  <house>
  <chairs count="3">
  <chairs>a</chairs>
  <chairs>b</chairs>
  <chairs>c</chairs>
  </chairs>
 </house>
如果我在*行写:

 Console.WriteLine(l), it will print me:

 a
 b
 c 
但是,如果我删除(*)并取消注释(**),在新创建的文件中,它只显示c,因为我认为它会因为串联而覆盖a和b。 可以获取所有值a、b、c并写入文件,而不仅仅是最后一个值

试试这个:

static void Main(string[] args)
{
   List<string> strings = new List<string>();
   using (XmlReader reader = XmlReader.Create("file.xml"))
    {
        while (reader.Read())
        {
            if (reader.IsStartElement())
            {
                switch (reader.Name)
                {

                    case "chairs":

                        if (reader.Read())
                        {
                            strings.Add(reader.Value.Trim());
                        }
                        break;
                }
            }
        }
    }
static void Main(字符串[]args)
{
列表字符串=新列表();
使用(XmlReader=XmlReader.Create(“file.xml”))
{
while(reader.Read())
{
if(reader.IsStartElement())
{
开关(reader.Name)
{
案例“椅子”:
if(reader.Read())
{
strings.Add(reader.Value.Trim());
}
打破
}
}
}
}

这样,列表中的
字符串将包含所有节点,并且可以用作集合。如果需要,您可以连接,但初始代码中的赋值会表明您每次仅覆盖该值。

有多种方法可以做到这一点

  • 在XmlReader上使用简单的迭代

  • 将Linq2Xml与Xpath结合使用


但是使用XMLs最方便的方法之一(当您事先知道模式时)是为您的模式使用和生成类,然后将xml转换为类并使用C#类。

谢谢。您的想法帮助了我。
static void Main(string[] args)
{
   List<string> strings = new List<string>();
   using (XmlReader reader = XmlReader.Create("file.xml"))
    {
        while (reader.Read())
        {
            if (reader.IsStartElement())
            {
                switch (reader.Name)
                {

                    case "chairs":

                        if (reader.Read())
                        {
                            strings.Add(reader.Value.Trim());
                        }
                        break;
                }
            }
        }
    }