Linq Query从此XML解析/获取内容(WSDL)

时间:2012-01-02 09:44:04

标签: .net c xml linq

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:def="http://www.w3.org/2002/ws/databinding/examples/6/09/"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<types>
      <xs:schema xmlns="http://www.w3.org/2002/ws/databinding/examples/6/09/"
                 targetNamespace="http://www.w3.org/2002/ws/databinding/examples/6/09/"
                 elementFormDefault="qualified">
         <xs:include xmlns:wsdl11="http://schemas.xmlsoap.org/wsdl/"
                     xmlns:soap11enc="http://schemas.xmlsoap.org/soap/encoding/"
                     schemaLocation="http://www.w3.org/2002/ws/databinding/examples/6/09/static/Included.xsd"/>
        <xs:include xmlns:wsdl11="http://schemas.xmlsoap.org/wsdl/"
                     xmlns:soap11enc="http://schemas.xmlsoap.org/soap/encoding/"
                     schemaLocation="http://www.w3.org/2002/ws/databinding/examples/6/09/static/Included.xsd"/>
         <xs:element name="echoInclude">
            <xs:complexType>
               <xs:sequence>
                  <xs:element ref="ex:include"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
      </xs:schema>
   </types>
</definitions>

在上面的XML中,我想获取xs:schema元素中每个xs:include元素的 schemaLocation 属性的值

1 个答案:

答案 0 :(得分:0)

首先,加载XML文档,设置Namespace并查询元素名称。

 XDocument doc = XDocument.Load(file);
 XNamespace ns= "http://www.w3.org/2001/XMLSchema";

 var nodeList = from ele in doc.Descendants(ns + "schema").Descendants(ns + "include")
                  select ele.Attribute("schemaLocation").Value;

 foreach (var t in nodeList)
  {
     Console.WriteLine(t);
  }