One method for reading and parsing XML data is to use System.Xml.XmlTextReader(). This will allow you to extract out exactly what you need from the XML.
This code example will demonstrate the basics of finding Elements and attributes. Since your situation doesn't require retrieving Attribute values, just remove the code for "if (reader.HasAttributes)".
The XmlTextReader class has many methods and properties that makes it very flexible.
Hope this helps.
string XML = "<?xml version=\"1.0\" encoding=\"utf-16\"?><ProcessResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><ExtensionData /><ErrorCode>0123</ErrorCode><ErrorMessage /><Message>xyz.</Message></ProcessResponse>";
System.Console.WriteLine(String.Format("\nOriginal XML String: {0}\n", XML));
if (XML != null && XML.Length > 0)
{
StringReader sReader = new StringReader(XML);
System.Xml.XmlTextReader xReader = new XmlTextReader(sReader);
loadXMLSettings(xReader);
xReader.Close();
}
private static void loadXMLSettings(XmlTextReader reader)
{
reader.WhitespaceHandling = WhitespaceHandling.None;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Console.WriteLine("Element <{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.WriteLine("Value: {0}", reader.Value);
break;
case XmlNodeType.EndElement:
Console.WriteLine("</{0}>", reader.Name);
break;
}
if (reader.HasAttributes)
{
Console.WriteLine("Attributes of <{0}>={1}",
reader.Name, reader.Value);
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToNextAttribute();
Console.WriteLine(" {0}={1}", reader.Name, reader.Value);
}
String attribValue = reader.GetAttribute("version");
Console.WriteLine("Attribute version has value of: {1}",
reader.Name, attribValue);
}
}
}
Results:
Original XML String: <?xml version="1.0" encoding="utf-16"?><ProcessResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><ExtensionData /><ErrorCode>0123</ErrorCode><ErrorMessage /><Message>xyz.</Message></ProcessResponse>
Attributes of <xml>=version="1.0" encoding="utf-16"
version=1.0
encoding=utf-16
Attribute version has value of: 1.0
Element <ProcessResponse>
Attributes of <ProcessResponse>=
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd=http://www.w3.org/2001/XMLSchema
Attribute version has value of:
Element <ExtensionData>
Element <ErrorCode>
Value: 0123
</ErrorCode>
Element <ErrorMessage>
Element <Message>
Value: xyz.
</Message>
</ProcessResponse>