It may sounds a very basic question, but here it is. I have this sample XML loaded in DOM
<message from="fromvalue" to="tovalue" xml:lang="en" type="chat">
<thread>{fe422b47-9856-4404-8c35-5ff45e43ee01}</thread>
<body>Test buddy</body>
<active xmlns="http://XXXX.org/protocol/chatstates" />
</message>
This i am receiving from request body using the following piece of code
StreamReader reader = new StreamReader ( HttpContext.Request.InputStream, System.Text.Encoding.UTF8 );
string sXMLRequest = reader.ReadToEnd ( );
XmlDocument xmlRequest = new XmlDocument ( );
xmlRequest.LoadXml ( sXMLRequest );
Now all i need to have is value of three things in three different variable
string bodytext = {body element inner text}
string msgfrom = {from attribute value of message element}
string msgto = {to attribute value of message element}
I am using C#, can anyone put some minutes from their precious time and guide me, will appreciate that highly
I’d use LINQ to XML here – it’s much simpler:
That will give you a value of
nullfor any element/attribute which isn’t present. If you’re happy with a NullReferenceException instead: