从XML中提取数据 [英] Extract data from XML

查看:85
本文介绍了从XML中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有以下XML出来,你能告诉我如何获得XML中一个名为ID_NUMERIC的字段的值,即711355 ..



我在字符串字段中获取此XML。



字段ID将保持不变,如果可能的话我怎样才能直接通过字段名称获取它。我在后面的代码中以粗体显示了所需的数据。请指教。



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 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



我尝试过:



生成XML并将其存储到字符串变量



MAzeem

What I have tried:

Generated XML and storing it to string variable

推荐答案

实现这一目标的方法很少。即:

1.使用 XmlSerialization [ ^ ]和XmlDeserialization

2.使用 XDocument类 [ ^ ]

3.使用 XmlDocument类 [ ^ ]



看看下面的代码(用法 XDocument class ):



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





详情请见:如何:使用后代方法(C#)查找单个后代


基于此处发布的内容,由于您的代码已经将XML转换为字符串,您是否考虑过使用正则表达式?这是一个假设包含ID_NUMERIC的字符串始终相同且只有每个XML一个ID_NUMERIC。



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.

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]);



输出:711355


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();


这篇关于从XML中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆