<?xml version="1.0" encoding="gbk" ?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns3:MoSOAPRespBody xmlns:ns3="http://smses.zjseol.com.cn/esesms/">
<ns3:message>
<id>id</id>
<result>处理结果</result>
</ns3:message>
</ns3:MoSOAPRespBody>
</soapenv:Body>
</soapenv:Envelope>怎么样取得id和result这两个节点的值?
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns3:MoSOAPRespBody xmlns:ns3="http://smses.zjseol.com.cn/esesms/">
<ns3:message>
<id>id</id>
<result>处理结果</result>
</ns3:message>
</ns3:MoSOAPRespBody>
</soapenv:Body>
</soapenv:Envelope>怎么样取得id和result这两个节点的值?
doc.Load("xx.xml"); XmlNodeList elemList = doc.GetElementsByTagName("id");
for (int i=0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
}
elemList = doc.GetElementsByTagName("result");
for (int i=0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
}
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns3:MoSOAPRespBody xmlns:ns3="http://smses.zjseol.com.cn/esesms/">
<ns3:message>
<id>id </id>
<result>处理结果 </result>
</ns3:message>
</ns3:MoSOAPRespBody>
</soapenv:Body>
</soapenv:Envelope> 是传递过来的一段字符串,不是一个xml文件
路径中具有非法字符。
??
{
DataTable dt = new DataTable(); if (Path != string.Empty)
{
DataSet ds = new DataSet();
try
{
ds.ReadXml(Path));
dt = ds.Tables["message"]; }
catch
{
throw new BusinessException("XML文件有问题,请导入正确的XML文件");
}
} }得到的dt就是你要的id 和 result 的记录表!
XmlDocument xml = new XmlDocument(); string source = "<?xml version=\"1.0\" encoding=\"gbk\" ?>" +
"<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"> " +
"<soapenv:Body> " +
"<ns3:MoSOAPRespBody xmlns:ns3=\"http://smses.zjseol.com.cn/esesms/\">" +
"<ns3:message>" +
"<id>id </id> " +
"<result>处理结果 </result> " +
"</ns3:message> " +
"</ns3:MoSOAPRespBody> " +
"</soapenv:Body> " +
"</soapenv:Envelope> "; byte[] bytes = Encoding.Default.GetBytes(source);
MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);
xml.Load(ms);
这是把你的字符串转成XmlDocument的...
然后你再从xmlDocument里取值就成了
<soapenv:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
<soapenv:Body>
<ns3:MoSOAPRespBody xmlns:ns3=""http://smses.zjseol.com.cn/esesms/"">
<ns3:message>
<id>id </id>
<result>处理结果 </result>
</ns3:message>
</ns3:MoSOAPRespBody>
</soapenv:Body>
</soapenv:Envelope> ";
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
foreach (XmlNode node in doc.GetElementsByTagName("id"))
Console.WriteLine(node.InnerXml);
foreach (XmlNode node in doc.GetElementsByTagName("result"))
Console.WriteLine(node.InnerXml);
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;namespace WindowsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); System.Xml.NameTable NT = new System.Xml.NameTable();
XmlNamespaceManager XNM = new XmlNamespaceManager(NT);
XNM.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
XNM.AddNamespace("ns3", "http://smses.zjseol.com.cn/esesms/");
XmlDocument Doc = new XmlDocument();
Doc.Load(@"c:\1.xml");
XmlNode Node = Doc.SelectSingleNode("/soapenv:Envelope/soapenv:Body/ns3:MoSOAPRespBody/ns3:message/id", XNM);
MessageBox.Show("id=" + Node.InnerText);
Node = Doc.SelectSingleNode("/soapenv:Envelope/soapenv:Body/ns3:MoSOAPRespBody/ns3:message/result", XNM);
MessageBox.Show("result=" + Node.InnerText);
}
}
}
XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath(Xml的路径);
XmlNode xmlnode=xml.SelectSingleNode("id");
string value=xmlnode.InnerText;
这样就得到你想要的值了,当然也可以使用linq
doc.Load("xx.xml"); XmlNodeList elemList = doc.DocumentElement.GetElementsByTagName("id");
for (int i=0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
}
elemList = doc.DocumentElement.GetElementsByTagName("result");
for (int i=0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
}