2013-02-05 2 views
7

IXmlNamespaceResolverオブジェクトを必要とするXElement.XPathSelectElements()オーバーロードを呼び出そうとしています。誰も私にIXmlNamespaceResolverを取得(または作成)する方法を教えてもらえますか?私はIXmlNamespaceResolverを取得するには

答えて

4

は、あなたがそのインターフェイス

使用new NameTable()を経由して、それにSystem.Xml.NameTableのインスタンスを渡し、XmlNameTableを取るコンストラクタを実装XmlNamespaceManagerを使用することができ、私のクエリで私が使用する名前空間のリストを持っています。その後、名前空間を追加するためにAddNamespace関数を呼び出すことができます。

var nsMgr = new XmlNamespaceManager(new NameTable()); 
nsMgr.AddNamespace("ex", "urn:example.org"); 
nsMgr.AddNamespace("t", "urn:test.org"); 
doc.XPathSelectElements("/ex:path/ex:to/t:element", nsMgr); 
+0

D'oh! - ありがとう。それはドキュメントからこれを見るための簡単な方法がないことは残念です。 – Andy

+10

パラメータのないコンストラクタはありません。 [this](http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.xmlnamespacemanager(v = vs.110).aspx)の記事を参照してください。 – Bernard

+0

別の質問への回答を参照してください。 – MonkeyWrench

10

使用new XmlNamespaceManager(new NameTable())

あなたは

var xDoc = XDocument.Parse(@"<m:Student xmlns:m='http://www.ludlowcloud.com/Math'> 
    <m:Grade>98</m:Grade> 
    <m:Grade>96</m:Grade> 
</m:Student>"); 

のような名前空間を使用するXML文書を持っている場合たとえば、あなたが使用する方法について検索しながら、私はこの記事を見つけた

var namespaceResolver = new XmlNamespaceManager(new NameTable()); 
namespaceResolver.AddNamespace("math", "http://www.ludlowcloud.com/Math"); 
var grades = xDoc.XPathSelectElements("//math:Student/math:Grade", namespaceResolver); 
4

を行うことによってGradeノードを取得することができますSOAP応答を処理するために[XPathSelectElements()]のオーバーロードが発生するため、いくつかの名前空間定義を持つ文書を持つ他の人にとって役に立ちます。

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
    <queryResponse xmlns="http://SomeUrl.com/SomeSection"> 
     <response> 
     <theOperationId>105</theOperationId> 
     <theGeneraltheDetailsCode>0</theGeneraltheDetailsCode> 
     <theDetails> 
      <aDetail> 
      <aDetailId>111</aDetailId> 
      <theValue>Some Value</theValue> 
      <theDescription>Some description</theDescription> 
      </aDetail> 
      <aDetail> 
      <aDetailId>222</aDetailId> 
      <theValue>Another Value</theValue> 
      <theDescription>Another description</theDescription> 
      </aDetail> 
      <aDetail> 
      <aDetailId>333</aDetailId> 
      <theValue>And another Value</theValue> 
      <theDescription>And another description</theDescription> 
      </aDetail> 
     </theDetails> 
     </response> 
    </queryResponse> 
    </soap:Body> 
</soap:Envelope> 

は[XDocument]作成するには::

var theDocumentXDoc = XDocument.Parse(theDocumentContent); 

を[てXmlNamespaceManager]を作成するには、私が使用:

var theNamespaceIndicator = new XmlNamespaceManager(new NameTable()); 
theNamespaceIndicator.AddNamespace("theSoapNS", "http://www.w3.org/2003/05/soap-envelope"); 
theNamespaceIndicator.AddNamespace("noSuffNS" , "http://SomeUrl.com/SomeSection"); 

名前を私の場合はこのような文書を持っています私は接頭辞( "theSoapNS"と "noSuffNS")は任意です。読み取り可能なコードに便利な名前を使用します。

[封筒]要素を選択するには、私が使用します。

var theEnvelope = theDocumentXDoc.XPathSelectElement("//theSoapNS:Envelope", theNamespaceIndicator); 

[queryResponse]要素を選択するには:

var theDetails = theDocumentXDoc.XPathSelectElement("//theSoapNS:Envelope/theSoapNS:Body/noSuffNS:queryResponse/noSuffNS:response/noSuffNS:theDetails", theNamespaceIndicator); 
:[theDetails]ですべての詳細を選択するには

var theQueryResponse = theDocumentXDoc.XPathSelectElement("//theSoapNS:Envelope/theSoapNS:Body/noSuffNS:queryResponse", theNamespaceIndicator); 

選択肢を使用したプログラム例(C#6):

//The usings you need: 
using System; 
using System.Linq; 
using System.Xml; 
using System.Xml.Linq; 
using System.Xml.XPath; 

namespace ProcessXmlCons 
{ 
    class Inicial 
    { 
     static void Main(string[] args) 
     { 
      new Inicial().Tests(); 
     } 

     private void Tests() 
     { 
      Test01(); 
     } 

     private void Test01() 
     { 
      var theDocumentContent = 
       @"<?xml version=""1.0"" encoding=""utf-8""?> 
        <soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> 
         <soap:Body> 
         <queryResponse xmlns=""http://SomeUrl.com/SomeSection""> 
          <response> 
           <theOperationId>105</theOperationId> 
           <theGeneraltheDetailsCode>0</theGeneraltheDetailsCode> 
           <theDetails> 
            <aDetail> 
             <aDetailId>111</aDetailId> 
             <theValue>Some Value</theValue> 
             <theDescription>Some description</theDescription> 
            </aDetail> 
            <aDetail> 
             <aDetailId>222</aDetailId> 
             <theValue>Another Value</theValue> 
             <theDescription>Another description</theDescription> 
            </aDetail> 
            <aDetail> 
             <aDetailId>333</aDetailId> 
             <theValue>And another Value</theValue> 
             <theDescription>And another description</theDescription> 
            </aDetail> 
           </theDetails> 
          </response> 
         </queryResponse> 
         </soap:Body> 
        </soap:Envelope> 
        "; 

      var theDocumentXDoc = XDocument.Parse(theDocumentContent); 
      var theNamespaceIndicator = new XmlNamespaceManager(new NameTable()); 
      theNamespaceIndicator.AddNamespace("theSoapNS", "http://www.w3.org/2003/05/soap-envelope"); 
      theNamespaceIndicator.AddNamespace("noSuffNS", "http://SomeUrl.com/SomeSection"); 

      var theEnvelope = theDocumentXDoc.XPathSelectElement("//theSoapNS:Envelope", theNamespaceIndicator); 
      Console.WriteLine($"The envelope: {theEnvelope?.ToString()} "); 

      var theEnvelopeNotFound = theDocumentXDoc.XPathSelectElement("//Envelope", theNamespaceIndicator); 
      Console.WriteLine("".PadRight(120, '_')); //Visual divider 
      Console.WriteLine($"The other envelope: \r\n {theEnvelopeNotFound?.ToString()} "); 

      var theQueryResponse = theDocumentXDoc.XPathSelectElement("//theSoapNS:Envelope/theSoapNS:Body/noSuffNS:queryResponse", theNamespaceIndicator); 
      Console.WriteLine("".PadRight(120, '_')); //Visual divider 
      Console.WriteLine($"The query response: \r\n {theQueryResponse?.ToString()} "); 

      var theDetails = theDocumentXDoc.XPathSelectElement("//theSoapNS:Envelope/theSoapNS:Body/noSuffNS:queryResponse/noSuffNS:response/noSuffNS:theDetails", theNamespaceIndicator); 
      Console.WriteLine("".PadRight(120, '_')); //Visual divider 
      Console.WriteLine($"The details: \r\n {theDetails?.ToString()} "); 
      Console.WriteLine("".PadRight(120, '_')); //Visual divider 

      foreach (var currentDetail in theDetails.Descendants().Where(elementToFilter => elementToFilter.Name.LocalName != "aDetail")) 
      { 
       Console.WriteLine($"{currentDetail.Name.LocalName}: {currentDetail.Value}"); 
      } 

      //Not optimal. Just to show XPath select within another selection: 
      Console.WriteLine("".PadRight(120, '_')); //Visual divider 
      Console.WriteLine("Values only:"); 
      foreach (var currentDetail in theDetails.Descendants()) 
      { 
       var onlyTheValueElement = currentDetail.XPathSelectElement("noSuffNS:theValue", theNamespaceIndicator); 
       if (onlyTheValueElement != null) 
       { 
        Console.WriteLine($"--> {onlyTheValueElement.Value}"); 
       } 
      } 
     } 

    } //class Inicial 
} //namespace ProcessXmlCons 
+0

ありがとう!これはまさに私がここに来たものです。 –

関連する問題