-4

I want to parse the list of XML using xmltextreader.

<xmllist>
<xml><item1>abc</item1><item2>xyz</item2></xml><xml><item1>abc</item1><item2>xyz</item2></xml>
</xmllist>

I want output separating each list item

xml1
Item1 abc
Item2 xyz

xml2
Item1 abc
Item2 xyz
Yogesh
  • 354
  • 2
  • 15
  • 3
    Possible duplicate of [Reading Xml with XmlReader in C#](https://stackoverflow.com/questions/2441673/reading-xml-with-xmlreader-in-c-sharp) – Liam Mar 05 '18 at 09:29
  • 1
    @liam it's not duplicate I want to parse list of item not a normal xml – Yogesh Mar 05 '18 at 09:31
  • What on earth is a list of item. It's either xml or it's not. Not to mention that this question lacks any effort what-so-ever – Liam Mar 05 '18 at 09:31
  • @liam u means to say above example of XML is not an xml – Yogesh Mar 05 '18 at 09:41
  • You said *not a normal xml* which isn't a thing – Liam Mar 05 '18 at 09:43
  • The above example is not xml (by definition). XML parsers and other XML tools have a problem with the data: it's not well-formed XML (= it's not XML). – Miro Lehtonen Mar 05 '18 at 11:17

1 Answers1

0

Try code below

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; }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20