积极答复者
这样的格式的xml如何解析啊?

问题
-
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <SearchStaffsResponse xmlns="http://tempuri.org/"> <SearchStaffsResult> <saas_user> <User_Code>string</User_Code> <User_No>string</User_No> <Company_ID>string</Company_ID> <Company_Name>string</Company_Name> <User_Name>string</User_Name> <User_PY>string</User_PY> <User_Pass>string</User_Pass> <User_Depart>string</User_Depart> <User_DepartName>string</User_DepartName> <User_Phone>string</User_Phone> <User_Mobile>string</User_Mobile> <User_EMail>string</User_EMail> <Is_Admin>int</Is_Admin> <User_Resume>string</User_Resume> <User_Photo>string</User_Photo> <User_Introduction>string</User_Introduction> <User_JoinDate>dateTime</User_JoinDate> <User_Remark>string</User_Remark> </saas_user> <saas_user> <User_Code>string</User_Code> <User_No>string</User_No> <Company_ID>string</Company_ID> <Company_Name>string</Company_Name> <User_Name>string</User_Name> <User_PY>string</User_PY> <User_Pass>string</User_Pass> <User_Depart>string</User_Depart> <User_DepartName>string</User_DepartName> <User_Phone>string</User_Phone> <User_Mobile>string</User_Mobile> <User_EMail>string</User_EMail> <Is_Admin>int</Is_Admin> <User_Resume>string</User_Resume> <User_Photo>string</User_Photo> <User_Introduction>string</User_Introduction> <User_JoinDate>dateTime</User_JoinDate> <User_Remark>string</User_Remark> </saas_user> </SearchStaffsResult> </SearchStaffsResponse> </soap:Body> </soap:Envelope>
这样带命名空间的xml该如何解析啊?我纠结了很久了
//初始化XML文档操作类
XmlDocument myDoc = new XmlDocument();
//加载XML文件
myDoc.LoadXml(xmlData);
UserSeachClass userClass = new UserSeachClass();
//搜索指定的节点
//xml有命名空间需加上命名空间格式
XmlNamespaceManager nsmgr = new XmlNamespaceManager(myDoc.NameTable);
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("xx", "http://tempuri.org/");
XmlNodeList xnl = myDoc.SelectNodes("soap:Envelope/soap:Body", nsmgr);我是这样写的但是如果soap:Body/SearchStaffResult xnl就变为空了
答案
-
namespace MyTest
{
public class MonthActualapp
{
public static void Main(string[] args)
{
//初始化XML文档操作类
XmlDocument myDoc = new XmlDocument();
//加载XML文件
myDoc.Load("abc.xml");
//搜索指定的节点
//xml有命名空间需加上命名空间格式
XmlNamespaceManager nsmgr = new XmlNamespaceManager(myDoc.NameTable);
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("ab", "http://tempuri.org/");
XmlNodeList xnl = myDoc.SelectNodes("//ab:SearchStaffsResult", nsmgr);
Console.WriteLine(xnl.Count);
}
}
}如果 XPath 表达式不包含前缀,则假定命名空间 URI 为空命名空间。
- 已编辑 ThankfulHeart 2012年4月16日 5:24
- 已标记为答案 Decker Dong - MSFT 2012年4月16日 7:07
- 取消答案标记 角落里的阳光 2012年4月16日 7:36
- 已标记为答案 角落里的阳光 2012年4月16日 7:36
全部回复
-
namespace MyTest
{
public class MonthActualapp
{
public static void Main(string[] args)
{
//初始化XML文档操作类
XmlDocument myDoc = new XmlDocument();
//加载XML文件
myDoc.Load("abc.xml");
//搜索指定的节点
//xml有命名空间需加上命名空间格式
XmlNamespaceManager nsmgr = new XmlNamespaceManager(myDoc.NameTable);
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("ab", "http://tempuri.org/");
XmlNodeList xnl = myDoc.SelectNodes("//ab:SearchStaffsResult", nsmgr);
Console.WriteLine(xnl.Count);
}
}
}如果 XPath 表达式不包含前缀,则假定命名空间 URI 为空命名空间。
- 已编辑 ThankfulHeart 2012年4月16日 5:24
- 已标记为答案 Decker Dong - MSFT 2012年4月16日 7:07
- 取消答案标记 角落里的阳光 2012年4月16日 7:36
- 已标记为答案 角落里的阳光 2012年4月16日 7:36