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>

Dani AI

Generated

Both and the replies from and show the core idea: the XML is small but namespace-aware, which is why simple element lookups can return null. The two common, complementary options below handle that namespace issue and the “I only have a stream/string from a web call” situation.

// namespace-aware XmlDocument + XPath
var doc = new XmlDocument();
doc.Load(yourStreamOrString);          // Load(stream) or LoadXml(string)
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("t", "http://tempuri.org/"); // replace with the document's namespace
var node = doc.SelectSingleNode("//t:string", nsmgr);
var value = node?.InnerText;

If you prefer LINQ-to-XML but want to avoid dealing with the exact namespace name (handy for tiny fragments or unknown namespaces):

var xdoc = XDocument.Load(yourStreamOrString);
var el = xdoc.Descendants().FirstOrDefault(e => e.Name.LocalName == "string");
var value = el?.Value;

Troubleshooting notes: check the document's root NamespaceURI (doc.DocumentElement.NamespaceURI) before querying; if the fragment is wrapped in a SOAP envelope, navigate into the Body element or add the SOAP namespace to your XmlNamespaceManager. If you see XmlException or odd characters, trim any leading BOM/whitespace and confirm the response encoding. Finally, if you can add a web/service reference (as suggested) that produces a typed client, prefer that—manual XML parsing is fine for quick fixes, but a proxy removes namespace/serialization hassles for production code.

Recommended Answers

All 2 Replies

>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=\"">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=\"">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.