Saturday, April 4, 2015

Parse XML File


  1. Reference System.XML
  2. Use XmlReader to parse the text string
  3. Use XmlReader to read the xml data

string xmlString =
                @"<bookstore>
                    <book genre='autobox' publishDate='2015-03-04'>
                        <title>Many bottle</title>
                        <author>
                            <firstName>David</firstName>
                            <lastName>Baby<lastName>
                        </author>
                    </book>
                  </bookstore>";

StringBuilder result = new StringBuilder();

using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
   reader.ReadToFollowing("book");
   reader.MoveToFirstAttribute();
   string genre = reader.Value;
   result.AppendLine("Genre value :" + genre);

   reader.ReadToFollowing("title");
   result.AppendLine("title element :" + reader.ReadElementContentAsString());
}

Console.Write(result);



No comments:

Post a Comment