从XML标签获取数据

时间:2019-01-11 08:49:49

标签: c# xml

对于这种XML,如何在c#中获取标记的数据

<soapenv:Envelope xmlns:stor="http://www.jboss.org/store" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <stor:FileNexusKey>
            <stor:TransRefGUID>FN_201918926275492952</stor:TransRefGUID>
            <stor:CertificateNumber>898976</stor:CertificateNumber>
            <stor:DocumentType>Name Change Request</stor:DocumentType>
            <stor:DocumentSubType>ULNAME</stor:DocumentSubType>
            <stor:DocumentID>5190071</stor:DocumentID>
            <stor:DOB>1999-02-22</stor:DOB>
            <stor:ClientFirstName>MS</stor:ClientFirstName>
            <stor:ClientLastName>Dhoni</stor:ClientLastName>
        </stor:FileNexusKey>
    </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

您可以尝试使用将XML转换为C#的服务,例如Xml2CSharp,在您的情况下,它会生成以下类型:

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="FileNexusKey", Namespace="http://www.jboss.org/store")]
    public class FileNexusKey 
    {
        [XmlElement(ElementName="TransRefGUID", Namespace="http://www.jboss.org/store")]
        public string TransRefGUID { get; set; }
        [XmlElement(ElementName="CertificateNumber", Namespace="http://www.jboss.org/store")]
        public string CertificateNumber { get; set; }
        [XmlElement(ElementName="DocumentType", Namespace="http://www.jboss.org/store")]
        public string DocumentType { get; set; }
        [XmlElement(ElementName="DocumentSubType", Namespace="http://www.jboss.org/store")]
        public string DocumentSubType { get; set; }
        [XmlElement(ElementName="DocumentID", Namespace="http://www.jboss.org/store")]
        public string DocumentID { get; set; }
        [XmlElement(ElementName="DOB", Namespace="http://www.jboss.org/store")]
        public string DOB { get; set; }
        [XmlElement(ElementName="ClientFirstName", Namespace="http://www.jboss.org/store")]
        public string ClientFirstName { get; set; }
        [XmlElement(ElementName="ClientLastName", Namespace="http://www.jboss.org/store")]
        public string ClientLastName { get; set; }
    }

    [XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body 
    {
        [XmlElement(ElementName="FileNexusKey", Namespace="http://www.jboss.org/store")]
        public FileNexusKey FileNexusKey { get; set; }
    }

    [XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope 
    {
        [XmlElement(ElementName="Header", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
        public string Header { get; set; }
        [XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName="stor", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Stor { get; set; }
        [XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenv { get; set; }
    }
}

然后您可以自己编写一个反序列化器(或使用NuGet和一般Internet上的众多反序列化器之一)

using System.Xml.Serialization;

public class Serializer
{       
    public T Deserialize<T>(string input) where T : class
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));

        using (StringReader reader = new StringReader(input))
        {
            return (T) serializer.Deserialize(reader);
        }
    }
}

,现在可以执行以下操作:

string xml = "..."; // Your XML here.

Envelope soapEnvelope = new Serializer().Deserialize<Envelope>(xml);  
FileNexusKey fileNexusKey = soapEnvelope.Body.FileNexusKey;

从XML中获取FileNexusKey对象。