本文介绍了从XML文档C#中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码在下面提到我将数据加载到xml文档中。
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(resty.Data);
doc.LoadXml(xml);
innser xml包含:
my code is mentioned below where i load the data into xml document.
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(resty.Data);
doc.LoadXml(xml);
innser xml contains:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><VerifyFingerPrintsResponse xmlns="http://NADRA.Biometric.Verification"><VerifyFingerPrintsResult><?xml version="1.0" encoding="utf-16"?>
<BIOMETRIC_VERIFICATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RESPONSE_DATA>
<RESPONSE_STATUS>
<CODE>175</CODE>
<MESSAGE>transaction id already exist</MESSAGE>
</RESPONSE_STATUS>
<SESSION_ID>12313</SESSION_ID>
<CITIZEN_NUMBER>4220106957335</CITIZEN_NUMBER>
</RESPONSE_DATA>
</BIOMETRIC_VERIFICATION></VerifyFingerPrintsResult></VerifyFingerPrintsResponse></s:Body></s:Envelope>
内文:
inner text :
<?xml version="1.0" encoding="utf-16"?>
<BIOMETRIC_VERIFICATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RESPONSE_DATA>
<RESPONSE_STATUS>
<CODE>175</CODE>
<MESSAGE>transaction id already exist</MESSAGE>
</RESPONSE_STATUS>
<SESSION_ID>12313</SESSION_ID>
<CITIZEN_NUMBER>4220106957335</CITIZEN_NUMBER>
</RESPONSE_DATA>
</BIOMETRIC_VERIFICATION>
我尝试过:
What I have tried:
i want to retrieve data from above xml kindly help me please
我将如何从上面的xml
how i will retrieve data from above xml
推荐答案
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="RESPONSE_STATUS")]
public class RESPONSE_STATUS {
[XmlElement(ElementName="CODE")]
public string CODE { get; set; }
[XmlElement(ElementName="MESSAGE")]
public string MESSAGE { get; set; }
}
[XmlRoot(ElementName="RESPONSE_DATA")]
public class RESPONSE_DATA {
[XmlElement(ElementName="RESPONSE_STATUS")]
public RESPONSE_STATUS RESPONSE_STATUS { get; set; }
[XmlElement(ElementName="SESSION_ID")]
public string SESSION_ID { get; set; }
[XmlElement(ElementName="CITIZEN_NUMBER")]
public string CITIZEN_NUMBER { get; set; }
}
[XmlRoot(ElementName="BIOMETRIC_VERIFICATION")]
public class BIOMETRIC_VERIFICATION {
[XmlElement(ElementName="RESPONSE_DATA")]
public RESPONSE_DATA RESPONSE_DATA { get; set; }
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName="xsd", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
}
}
接下来是将类映射到Xml数据:
Next is to map the classes to the Xml data:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Xml2CSharp
{
public static class XmlConverter
{
public static T ToClass<T>(string data)
{
var response = default(T);
if (!string.IsNullOrEmpty(data))
{
var settings = new XmlReaderSettings() { IgnoreWhitespace = true };
var serializer = XmlSerializerFactoryNoThrow.Create(typeof(T));
var reader = XmlReader.Create(new StringReader(data), settings);
response = (T)Convert.ChangeType(serializer.Deserialize(reader), typeof(T));
}
return response;
}
}
// ref: http://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor/39642834#39642834
public static class XmlSerializerFactoryNoThrow
{
public static Dictionary<Type, XmlSerializer> cache = new Dictionary<Type, XmlSerializer>();
private static object SyncRootCache = new object();
public static XmlSerializer Create(Type type)
{
XmlSerializer serializer;
lock (SyncRootCache)
if (cache.TryGetValue(type, out serializer))
return serializer;
lock (type) //multiple variable of type of one type is same instance
{
//constructor XmlSerializer.FromTypes does not throw the first chance exception
serializer = XmlSerializer.FromTypes(new[] { type })[0];
//serializer = XmlSerializerFactoryNoThrow.Create(type);
}
lock (SyncRootCache) cache[type] = serializer;
return serializer;
}
}
}
使用:
var data = XmlConverter.ToClass<BIOMETRIC_VERIFICATION>(xml);
注意:我没有运行此代码但应该可以工作。
Note: I have not run this code but should work.
这篇关于从XML文档C#中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!