[英]XML Deserialize and return part of the object as XML
Dont know even how to search for this, but when I deserialize an xml string, I want it to return the first level of the XML: 甚至都不知道如何搜索,但是当我反序列化xml字符串时,我希望它返回XML的第一级:
String: <pkt><cmd>logreq</cmd><data><name>scott</name><password>abc123</password><designator>R233</designator><token>123456</token></data></pkt>
字符串:
<pkt><cmd>logreq</cmd><data><name>scott</name><password>abc123</password><designator>R233</designator><token>123456</token></data></pkt>
When I use this code: 当我使用此代码时:
class xmlFunctions
{
public Cmd GetCmd(string sXML)
{
Cmd cmd = new Cmd();
try
{
var sr = new StringReader(sXML);
var xs = new XmlSerializer(typeof(Cmd));
cmd = (Cmd)xs.Deserialize(sr);
return cmd;
}
catch (Exception)
{
Console.WriteLine("Cannot Parse Input.");
}
return cmd;
}
}
and this is my Cmd object: 这是我的Cmd对象:
[XmlRoot("pkt")]
public class Cmd
{
public string cmd { get; set; }
public string data { get; set; }
}
I would expect that Cmd.cmd = logreq and Cmd.data = <name>scott</name><password>abc123</password><designator>R233</designator><token>123456</token>
我希望Cmd.cmd = logreq和Cmd.data =
<name>scott</name><password>abc123</password><designator>R233</designator><token>123456</token>
but the data = scott. 但数据=斯科特。
How do I get it to return the XML part above? 如何获取返回上面的XML部分的信息? it seems that it wants to deserialize all the xml and not just the first level?
似乎它想反序列化所有xml,而不仅是第一层?
Thanks. 谢谢。
I think you really mean Parse a XML string and return the root
node. 我觉得你真的是一个解析XML字符串,并返回
root
节点。 In that case this is what you're looking for How to Parse XML with XmlReader 在这种情况下,这就是您要寻找的如何使用XmlReader解析XML的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.