Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I had the following XML coming out, Can you please advise how can i get the value for a field in XML called ID_NUMERIC i.e. 711355..

I am getting this XML in string field.

The field ID will remain constant so if possible how can i get it by field name directly. I made the required data in bold in the succeeding code . Please advise.

XML
<?xml version="1.0" encoding="utf-8"?><limsaaaml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://sssssss">
<header>
<parameter name="SESSION">    209103</parameter>
</header>

<body>
<transaction id="TRAN1"><system response_type="system"><entity type="SAMPLE"><actions><action><command>CREATE</command><parameter name="TEMPLATE">AZEEM</parameter></action></actions>
<fields>
<field id="AREA" direction="in" datatype="Text">XXXX</field>
<field id="SSSSS" direction="in" datatype="Text">TFFFLE</field>
<field id="CFFFY" direction="in" datatype="Text">TLsss-0</field>
<field id="POINT" direction="in" datatype="Text">ssssse-110</field>
<field id="LssAsssB" direction="in" datatype="Text">saasdad</field>
<field id="s_DATE" direction="in" datatype="Date">9/17/2018 12:31:10 AM</field>
<field id="PRIORITY" direction="in" datatype="Text">3</field>
<field id="REASON" direction="in" datatype="Text">Reason</field>
<field id="COLLECTED" direction="in" datatype="Text">asdasad</field>
<field id="RI" direction="in" datatype="Text">hhhhhh</field>
<field id="dddddd" direction="in" datatype="Text">asdasda</field>
<field id="SCOMMENTS" direction="in" datatype="Text">COMMENTS</field>
<field id="AscaeeeDddddUCT" direction="in" datatype="Text">Bla</field>
<field id="ID_NUMERIC" direction="out" datatype="Text">    711355</field>
</fields><children><entity type="TEST"><actions /><fields>
<field id="Hut" direction="in" datatype="Text">Base</field>
<field id="COMPONENT_LIST" direction="in" datatype="Text">D</field>

</fields><children />
</entity></children></entity></system>
</transaction></body><errors /></limsaaaml>


MAzeem

What I have tried:

Generated XML and storing it to string variable
Posted
Updated 17-Sep-18 9:19am
v2

There's few ways to achieve that. ie.:
1. using XmlSerialization[^] and XmlDeserialization
2. using XDocument class[^]
3. using XmlDocument class[^]

Take a look at below code (usage of XDocument class):

string xmlcontent = @"<?xml version='1.0' encoding='utf-8'?>
<limsaaaml xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://sssssss'>
<header>
	<parameter name='SESSION'>    209103</parameter>
</header>
<body>
<transaction id='TRAN1'>
<system response_type='system'>
<entity type='SAMPLE'>
<actions>
	<action>
		<command>CREATE</command>
		<parameter name='TEMPLATE'>AZEEM</parameter>
	</action>
</actions>
<fields>
	<field id='AREA' direction='in' datatype='Text'>XXXX</field>
	<field id='SSSSS' direction='in' datatype='Text'>TFFFLE</field>
	<field id='CFFFY' direction='in' datatype='Text'>TLsss-0</field>
	<field id='POINT' direction='in' datatype='Text'>ssssse-110</field>
	<field id='LssAsssB' direction='in' datatype='Text'>saasdad</field>
	<field id='s_DATE' direction='in' datatype='Date'>9/17/2018 12:31:10 AM</field>
	<field id='PRIORITY' direction='in' datatype='Text'>3</field>
	<field id='REASON' direction='in' datatype='Text'>Reason</field>
	<field id='COLLECTED' direction='in' datatype='Text'>asdasad</field>
	<field id='RI' direction='in' datatype='Text'>hhhhhh</field>
	<field id='dddddd' direction='in' datatype='Text'>asdasda</field>
	<field id='SCOMMENTS' direction='in' datatype='Text'>COMMENTS</field>
	<field id='AscaeeeDddddUCT' direction='in' datatype='Text'>Bla</field>
	<field id='ID_NUMERIC' direction='out' datatype='Text'>    711355</field>
</fields>
<children>
	<entity type='TEST'><actions />
	<fields>
		<field id='Hut' direction='in' datatype='Text'>Base</field>
		<field id='COMPONENT_LIST' direction='in' datatype='Text'>D</field>
	</fields>
<children />
</entity>
</children>
</entity>
</system>
</transaction>
</body>
<errors/>
</limsaaaml>";


XDocument xdoc = XDocument.Load(XmlReader.Create(new StringReader(xmlcontent)));
XNamespace s = "http://sssssss";
var f = xdoc.Descendants(s + "field").Where(x=>(string)x.Attribute("id")=="ID_NUMERIC").SingleOrDefault().Value.Trim();
Console.WriteLine("{0}", f);
//prints: 711355


For further details, please see: How to: Find a Single Descendant Using the Descendants Method (C#)
 
Share this answer
 
Comments
Bryian Tan 17-Sep-18 11:21am    
Nice! Why don't I though of that :)
Maciej Los 17-Sep-18 12:41pm    
Thank you, Bryian.
Richard Deeming 18-Sep-18 14:34pm    
.SingleOrDefault().Value.Trim()


SingleOrDefault will return null if there are no matching elements. In which case, the call to .Value would throw a NullReferenceException. You'll probably want a null-conditional operator[^] in there.

You can also combine the Where and SingleOrDefault into a single call:
var f = xdoc.Descendants(s + "field").SingleOrDefault(x => (string)x.Attribute("id") == "ID_NUMERIC")?.Value?.Trim();
Maciej Los 18-Sep-18 14:44pm    
Good point!
Thanks, Richard.
Based on what posted here, since your code already converting the XML into string, have you thought of using regular expression? Here is an example with the assumption that the string that wrap around the ID_NUMERIC is always identical and only one ID_NUMERIC per XML.

C#
string sampleXmlText= "<field id=\"AscaeeeDddddUCT\" direction=\"in\" datatype=\"Text\">Bla</field><field id=\"ID_NUMERIC\" direction=\"out\" datatype=\"Text\">    711355</field></fields><children><entity type=\"TEST\"><actions /><fields>";
		
		Console.WriteLine(Regex.Matches(sampleXmlText,  "(?<=<field id=\"ID_NUMERIC\" direction=\"out\" datatype=\"Text\"> )(.*?)(?=</field>)")[0]);

Output: 711355
 
Share this answer
 
Comments
Maciej Los 17-Sep-18 5:12am    
5ed!
Hi, 

Just last thing in XML i also had a node in XML somewhere like:

<STATUS>V</STATUS>

I am trying to get the value in string with the following code but its giving error am i missing something thanks. 

<pre>string ID= (Regex.Matches(ProcessedXML, "(?<=<STATUS> )(.*?)(?=</STATUS>)")[0]).ToString();
 
Share this answer
 
Comments
Bryian Tan 17-Sep-18 16:10pm    
Please append to the comment or original post instead of posting as a solution. By the way, make sure the STATUS is upper case, because they are case sensitive and remove the extra space in the regular expression.
string ID= (Regex.Matches(ProcessedXML, "(?<=<STATUS>)(.*?)(?=</STATUS>)")[0]).ToString();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900