Search code examples
c#xmlreader

How to get the contents of the first found node using xmlreader?


If I have a xml file like this

<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
   <product description="Cardigan Sweater" product_image="cardigan.jpg">
      <catalog_item gender="Men's">
         <item_number>QWZ5671</item_number>
         <price>39.95</price>
         <size description="Medium">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
         </size>
         <size description="Large">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
         </size>
      </catalog_item>
      <catalog_item gender="Women's">
         <item_number>RRX9856</item_number>
         <price>42.50</price>
         <size description="Small">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
         </size>
         <size description="Medium">
            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
            <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            <color_swatch image="black_cardigan.jpg">Black</color_swatch>
         </size>
         <size description="Large">
            <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
            <color_swatch image="black_cardigan.jpg">Black</color_swatch>
         </size>
         <size description="Extra Large">
            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            <color_swatch image="black_cardigan.jpg">Black</color_swatch>
         </size>
      </catalog_item>
   </product>
</catalog>

How do I get the value of the first price node using xmlreader? I tried the below code but it does not do what I want it to...

XmlReaderSettings settings=new XmlReaderSettings();
settings.DtdProcessing=DtdProcessing.Ignore;
XmlReader reader=XmlReader.Create(@"D:\abc.xml",settings);
while (reader.Read())
{
    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name=="price"))
    {
        Console.WriteLine(reader.ReadInnerXml().First());
    }
}

Console.ReadLine();

What am I missing here?

Also I heard that xmlreader is better than Xdocument for reading and writing large xml files, how large does it have to be to slow down or crash my program if I run it on a PC with a simple dual core cpu and 1-2 GB of ram? I want to read and modify multiple xml files using a foreach loop and open each xml files one by one and perform the reading/modification..


Solution

  • Your code is returning the first character of each price element; you want to return all of the first price element and then stop.

    while (reader.Read())
    {
        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "price"))
        {
            Console.WriteLine(reader.ReadInnerXml());
            break;
        }
    }