日期:2014-05-20  浏览次数:20827 次

获取字符串装 的 类容
此字符串格式为
<input cmdcode = “0x0416” deviceid = “?” userid = “?”>
<userid>0</userid>
<pwd>0</pwd>
</input>
如何获取
<userid>
<pwd>
的值
是把字符串转换成XML在进行获取吗?

------解决方案--------------------
用C#读xml

XmlDocument doc = new XmlDocument();
doc.Load("aaa.xml");
XmlNode userIDNode = doc.SelectSingleNode("input").SelectSingleNode("userid");
XmlNode pwdNode = doc.SelectSingleNode("input").SelectSingleNode("pwd");
string userid = userIDNode.InnerText;
string pwd = pwdNode.InnerText;
Console.Write("userid:"+userid+",pwd:"+pwd);
------解决方案--------------------
C# code
            string str = @"<input cmdcode = ""0x0402"" deviceid = ""?"" userid = ""?"">
<orderserial>00000000</orderserial>
<ordertype>0</ordertype>
<guestid>0</guestid>
<tablemark/>
<pce>0</pce>
<dishlist number =""9"">
<dish id=""010301001"" number=""100"" description="""">aaaaa</dish>
<dish id=""010301002"" number=""100"" description=""bbbb""/>
<dish id=""010301003"" number=""100"" description=""""/>
<dish id=""010301004"" number=""100"" description=""""/>
<dish id=""010301005"" number=""100"" description=""""/>
<dish id=""010301006"" number=""100"" description=""""/>
<dish id=""010301007"" number=""100"" description=""""/>
<dish id=""010301008"" number=""100"" description=""""/>
<dish id=""010301009"" number=""100"" description=""""/>
</dishlist>
</input>";
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(str);
            XmlNode dish = xml.SelectSingleNode("//dish[@id='010301001']");
            Console.WriteLine(dish.InnerText);
            XmlNode dish2 = xml.SelectSingleNode("//dish[@id='010301002']");
            Console.WriteLine(dish2.Attributes["description"].Value);