40321

How to get specific value from a xml string in c#

I have following string

<SessionInfo> <SessionID>MSCB2B-UKT3517_f2823910df-5eff81-528aff-11e6f-0d2ed2408332</SessionID> <Profile>A</Profile> <Language>ENG</Language> <Version>1</Version> </SessionInfo>

now I want to get the <strong>value</strong> of <strong>SessionID</strong>. I tried with below ..

var rootElement = XElement.Parse(output);//output means above string and this step has values

but in here,,

var one = rootElement.Elements("SessionInfo");

it didn't work.what can I do that.

and What if the xml string like below.can we use same to get the <strong>sessionID</strong>

<DtsAgencyLoginResponse xmlns="DTS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="DTS file:///R:/xsd/DtsAgencyLoginMessage_01.xsd"> <SessionInfo> <SessionID>MSCB2B-UKT351ff7_f282391ff0-5e81-524548a-11eff6-0d321121e16a</SessionID> <Profile>A</Profile> <Language>ENG</Language> <Version>1</Version> </SessionInfo> <AdvisoryInfo /> </DtsAgencyLoginResponse>

Answer1:

rootElement already references <SessionInfo> element. Try this way :

var rootElement = XElement.Parse(output); var sessionId = rootElement.Element("SessionID").Value;

Answer2:

You can select node by xpath and then get value:

XmlDocument doc = new XmlDocument(); doc.LoadXml(@"<SessionInfo> <SessionID>MSCB2B-UKT3517_f2823910df-5eff81-528aff-11e6f-0d2ed2408332</SessionID> <Profile>A</Profile> <Language>ENG</Language> <Version>1</Version> </SessionInfo>"); string xpath = "SessionInfo/SessionID"; XmlNode node = doc.SelectSingleNode(xpath); var value = node.InnerText;

Answer3:

Please do not do this manually. This is horrible. Use .NET built in stuff to make it simpler and more reliable

XML Serialisation

This is the right way to do it. You create Classes and let them be serialized automatically from an XML string.

Answer4:

Try this method :

private string parseResponseByXML(string xml) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlNodeList xnList = xmlDoc.SelectNodes("/SessionInfo"); string node =""; if (xnList != null && xnList.Count > 0) { foreach (XmlNode xn in xnList) { node= xn["SessionID"].InnerText; } } return node; }

Your nodes :

xmlDoc.SelectNodes("/SessionInfo");

Different sample

xmlDoc.SelectNodes("/SessionInfo/node/node");

I hope it helps.

Recommend

  • How to send a javascript array to cherrypy
  • Use of pushd and popd command with UNC path
  • Display Json data in table format using Jquery
  • Getting “Not logged on in interface XBP” error when calling XBP function module via sap .net connect
  • Unable to send mail from SSIS using script task
  • Result is undefined when I use form-data in Postman
  • CSRF protection techniques
  • Magento, i need define first product image as thumbnail
  • Hive from JSON Error
  • Python/Flask: How to tell how long a user spends on a page? (Data entry/time log app)
  • How Do I Prevent Users From Modifying Resources They Do Not Own?
  • How do I correctly setup a GKSession (Bluetooth) on iOS 6.1
  • Send CTRL_C/SIGINT to a process from C#
  • how to store sql query result in a variable and messegeBox
  • Server does not starts with spring app [duplicate]
  • InProc session data disapearing
  • Sending commands to stdin and sending end of transmit (Ctrl+D)
  • converting curl call to python requests
  • SessionInformation expiration doesn't lead to user log out from system
  • Reading contents of a managed bean with reflection in a JSF application
  • how to ignore xml namespaces?
  • Should a web service response include empty values?
  • Splash Screen will not display
  • JBoss External Properties Files in Classpath
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Pass value from viewmodel to script in zk
  • Optimizing database types to compact database (SQLite)
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Cross-Platform Protobuf Serialization
  • When should I choose bucket sort over other sorting algorithms?
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Unanticipated behavior
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • How to get icons for entities from eclipse?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • How to load view controller without button in storyboard?