Hello all,

I am trying to read value from a XML message fragment using C# and I am unable to load and read the document mainly because I don't enough experience working with xml data. I don't have the raw xml document on hard drive, so it is generated from a web request from another class.

Can anyone help with this.

thx.

below is a sample of the fragmented message. I would like to parse it via C# inside a class that I am creating.

<?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://tempuri.org/">[B]6264021[/B]</string>

>I would like to parse it via C# inside a class that I am creating.

Document Object Model - classes of System.Xml namespace.

string str="<?xml version=\"1.0\" encoding=\"utf-8\" ?><string xmlns=\"http://tempuri.org/\">6264021</string>";

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(str);

var value = doc.GetElementsByTagName("string");

if(value.Count!=0)
        Console.WriteLine(value[0].InnerText);

Another way to read that is to use LINQ to XML (System.Xml.Linq).

string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><string xmlns=\"http://tempuri.org/\">6264021</string>";
XDocument document = XDocument.Parse(xml);
XNamespace tempuri = "http://tempuri.org/";
var result = document.Element(tempuri + "string").Value;
Console.WriteLine(result);

Same number of lines to retrieve that simple value as the previous method, but can also be used to parse and load larger XML documents into collections of defined objects or even anonymous types.

But more to the point, the XML you've submitted here looks to be the result of a web service call. As such, I wonder why you are having to parse it? Can you not establish a web reference to the service and then obtain the string value by using a web service client?

commented: Yes. Cool! +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.