读书人

获取Xml值解决思路

发布时间: 2012-12-26 14:39:29 作者: rapoo

获取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 很方便~~
[其他解释]

引用:
string test = @"<Message>
<MyMsgId>2d0eb6d2-9331-404c-ab2e-77c062562d75</MyMsgId>
<DeptCode>U320500001</DeptCode>
<SendMobile>13771701599</SendMobile>
<ReciveMobiles>137717……


可以的

顺便补充下,如果你的xml 有 xmlns="http://....../" 的话

你要记得用 SelectSingleNode("prafix:DeptCode",xmlnsmanager).

的形式,要不你读不到节点。
[其他解释]
xml增加删除更新操作 百度 OK?
------其他解决方案--------------------


该回复于2012-12-11 09:00:57被管理员删除

读书人网 >asp.net

热点推荐