解析XML列表c#

解析XML列表c#,c#,xml,xml-parsing,C#,Xml,Xml Parsing,我想使用xmltextreader解析XML列表 <xmllist> <xml><item1>abc</item1><item2>xyz</item2></xml><xml><item1>abc</item1><item2>xyz</item2></xml> </xmllist> 请尝试下面的代码 using System; u

我想使用xmltextreader解析XML列表

<xmllist>
<xml><item1>abc</item1><item2>xyz</item2></xml><xml><item1>abc</item1><item2>xyz</item2></xml>
</xmllist>
请尝试下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication29
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlTextReader.Create(FILENAME);
            List<Xml> xmls = new List<Xml>();
            while (!reader.EOF)
            {
                if (reader.Name != "xml")
                {
                    reader.ReadToFollowing("xml");
                }
                if (!reader.EOF)
                {
                    XElement xml = (XElement)XElement.ReadFrom(reader);
                    Xml item = new Xml() { item1 = (string)xml.Element("item1"), item2 = (string)xml.Element("item2")};
                    xmls.Add(item);
                }
            }
        }
    }
    public class Xml
    {
        public string item1 { get; set; }
        public string item2 { get; set; }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序29
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XmlReader=XmlTextReader.Create(文件名);
List xmls=新列表();
而(!reader.EOF)
{
if(reader.Name!=“xml”)
{
reader.ReadToFollowing(“xml”);
}
if(!reader.EOF)
{
XElement xml=(XElement)XElement.ReadFrom(reader);
Xml item=newxml(){item1=(string)Xml.Element(“item1”),item2=(string)Xml.Element(“item2”)};
添加(项目);
}
}
}
}
公共类Xml
{
公共字符串item1{get;set;}
公共字符串item2{get;set;}
}
}

可能重复的@liam it's not duplicate我想解析项目列表不是一个普通的XML项目列表到底是什么项目列表。要么是xml,要么不是。更不用说这个问题缺乏任何努力了,怎么回事-ever@liam你的意思是说上面的XML示例不是一个XML,你说的不是一个普通的XML,这不是一件事
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication29
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlTextReader.Create(FILENAME);
            List<Xml> xmls = new List<Xml>();
            while (!reader.EOF)
            {
                if (reader.Name != "xml")
                {
                    reader.ReadToFollowing("xml");
                }
                if (!reader.EOF)
                {
                    XElement xml = (XElement)XElement.ReadFrom(reader);
                    Xml item = new Xml() { item1 = (string)xml.Element("item1"), item2 = (string)xml.Element("item2")};
                    xmls.Add(item);
                }
            }
        }
    }
    public class Xml
    {
        public string item1 { get; set; }
        public string item2 { get; set; }
    }

}