反序列化xml字符串作为字符串元素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> 
alan0633 回答:反序列化xml字符串作为字符串元素c#

序列化将仅针对单个元素的文本。您最好使用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;
本文链接:https://www.f2er.com/3162207.html

大家都在问