C# - parsing an XML string

C# - parsing an XML string

Post by Donalban » Sun, 10 Jul 2005 05:37:29



Hello,

I am developing in Visual Studio .NET 2005.  I am attempting to parse
an XML string by using the example given in the help files:

ms-help://MS.VSCC.v80/MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref/html/M_System_Xml_XmlReader_Create_1_6b76289f.htm

namely,

string xmlData ="<item productID='124390'>" +
                        "<price>5.95</price>" +
                        "</item>";

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(new StringReader(xmlData));

However, when I attempt to actually parse this string by

reader.ReadStartElement("item");
string productID = reader.GetAttribute("productID");

I just get null for productID.  Can anyone see what I am doing wrong?

Thanks,
Don

 
 
 

C# - parsing an XML string

Post by Nilesh.Des » Sun, 10 Jul 2005 06:22:33


Attribute values must be in double quotes for xml to be well formed.

Change following code

string xmlData ="<item productID=\"124390\">" +
                        "<price>5.95</price>" +
                        "</item>";

 
 
 

C# - parsing an XML string

Post by Donalban » Sun, 10 Jul 2005 06:40:12


Thanks.  I also had to change reader.ReadStartElement("item");

to

reader.MoveToContent();

to get it to work.

Don