日期:2014-05-17 浏览次数:20483 次
<?xml version="1.0" standalone="yes"?> <users> <xs:schema id="users" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="users" msdata:IsDataSet="true" msdata:Locale="en-US"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="user"><!--这里是表名--> <xs:complexType> <xs:sequence> <xs:element name="userName" type="xs:string" minOccurs="0" msdata:Ordinal="0" /> <xs:element name="userPass" type="xs:string" minOccurs="0" msdata:Ordinal="1" /> </xs:sequence> <xs:attribute name="id" type="xs:int" /> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> </users>
string s = File.ReadAllText(Server.MapPath("~/test.xml")); string xmlStr = s.Replace("xs:", ""); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlStr); XmlNodeList names = xmlDoc.SelectNodes(@"//element[@name='user']//*/@name"); foreach (XmlNode name in names) Response.Write(name.Value + "<br/>");
------解决方案--------------------
XmlDocument xDoc = new XmlDocument(); xDoc.Load("..\\..\\test2.xml"); XmlNamespaceManager xnm = new XmlNamespaceManager(xDoc.NameTable); xnm.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); XmlNodeList xNodes = xDoc.DocumentElement.SelectNodes("//*/xs:choice/xs:element/*//*[@name]", xnm); foreach (XmlNode item in xNodes) { Console.WriteLine(item.Attributes["name"].Value); } //输出: //userName //userPass //id