繁体   English   中英

反序列化 xml 字符串作为字符串元素 c#

[英]deserialization xml string as string element c#

我有一个像这样的 xml 字符串:

<root>
    <header>
        <reqType>REQUEST</reqType>
        <priority>1</priority>
        <channel>TTP</channel>
        <synasyn>true</synasyn>
        <zip>false</zip>
    </header>
    <body>
        <command>getCustInfo</command>
        <customerID>14231131</customerID>
    </body>
</root>

我的代码如下

[XmlRoot ("root")]
public class root
{
    public header header;
    [XmlElement("body")]
    public string body;
}
[XmlRoot("header")]
public class header
{
    [DataMember(Order = 1, IsRequired = true)]
    public string reqType;
    .....
}

当我反序列化上面的 xml 字符串时: root data = CompressHelper.fromXML<root>(xml); 我得到了一个结果“body”元素是“getCustInfo”所以问题是我如何反序列化以将结果“body”作为字符串值:

<command>getCustInfo</command>
<customerID>14231131</customerID> 

序列化将仅针对单个元素的文本。 你最好使用 Xpath 来获得你想要的 xml 标签的文本。 下面是你如何做到这一点

 string xml = @"<root>
                   <header>
                       <reqType>REQUEST</reqType>
                       <priority>1</priority>
                       <channel>TTP</channel>
                       <synasyn>true</synasyn>
                       <zip>false</zip>
                   </header>
                   <body>
                       <command>getCustInfo</command>
                       <customerID>14231131</customerID>
                   </body>
                </root>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
XmlNode body = xdoc.SelectSingleNode("/root/body");
string bodyInnerXML = body.InnerXml;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM