获取Xml值
<?xml version="1.0" encoding="gb2312" ?>
<Message>
<MyMsgId>2d0eb6d2-9331-404c-ab2e-77c062562d75</MsgId>
<Code>U320500001</DeptCode>
<SendMobile>13771701599</SendMobile>
<ReciveMobiles>13771701599,13001000058</ReciveMobiles>
<Content>Hello World</Content>
<SysType>12</SysType>
</Message>
怎么获取这Xml中的值?我想获取出来保存到数据裤里面去
[最优解释]
string test = @"<Message>
<MyMsgId>2d0eb6d2-9331-404c-ab2e-77c062562d75</MyMsgId>
<DeptCode>U320500001</DeptCode>
<SendMobile>13771701599</SendMobile>
<ReciveMobiles>13771701599,13001000058</ReciveMobiles>
<Content>Hello World</Content>
<SysType>12</SysType>
</Message>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(test);
XmlNodeList xnl = xmlDoc.SelectNodes("Message");
foreach (XmlNode linkNode in xnl)
{
XmlElement xe = (XmlElement)linkNode;//将子节点类型转换为XmlElement类型
string aa = xe.SelectSingleNode("MyMsgId").InnerText.Trim();
string bb = xe.SelectSingleNode("DeptCode").InnerText.Trim();
Response.Write(aa);
Response.Write(bb);
}
输出:
MyMsgId:2d0eb6d2-9331-404c-ab2e-77c062562d75
DeptCode U320500001
<MyMsgId>2d0eb6d2-9331-404c-ab2e-77c062562d75</MsgId>
<Code>U320500001</DeptCode>
这里的你的结束节点名称不对。
[其他解释]
你应该先去看 xmldocument或xdocument的帮助
[其他解释]
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XmlFile.xml"));
XmlNode xn = xmlDoc.SelectSingleNode("scanpath");//查找<scanpath>
XmlElement xe = (XmlElement)xn;
string ipp = xe.GetAttribute("attr").ToString();
[其他解释]
XmlDocument或者XmlTextReader
[其他解释]
linq to xml 很方便~~
[其他解释]