IXmlNamespaceResolverオブジェクトを必要とするXElement.XPathSelectElements()オーバーロードを呼び出そうとしています。誰も私にIXmlNamespaceResolverを取得(または作成)する方法を教えてもらえますか?私はIXmlNamespaceResolverを取得するには
答えて
は、あなたがそのインターフェイス
使用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);
使用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);
を行うことによって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
ありがとう!これはまさに私がここに来たものです。 –
- 1. 取得値のカウントの値を取得するには...
- 2. 最新のCanExecute値を取得するReativeCommandを取得するには?
- 3. PHPでURLを取得するには
- 4. CGRectでUIViewを取得するには?
- 5. ウェブページのコンテンツを取得するには
- 6. RewriteCondでローカルパスを取得するには?
- 7. iOSでタッチサイズを取得するには?
- 8. paypal APIクレデンシャルを取得するには?
- 9. ターゲットURLを取得するには?
- 10. GitPythonでリモートリポジトリを取得するには?
- 11. 電子メールアドレスを取得するには
- 12. アンドロイドデバイスのカメラセンサーサイズを取得するには?
- 13. クロスレコードを取得するには?
- 14. javascriptオブジェクトメソッド名を取得するには?
- 15. 登録トークンを取得するには?
- 16. HIT IDを取得するには?
- 17. メールキットでuidを取得するには?
- 18. Silvlerlightでストレッチを取得するには?
- 19. ファンクションセグを取得するには?
- 20. Javascript:URLのコンテンツを取得するには?
- 21. EJBリスニングポートを取得するには?
- 22. GeoToolsでマウスコーディネーションを取得するには?
- 23. Androidでビデオフレームレートを取得するには?
- 24. このスクロールトリガーエフェクトを取得するには
- 25. 値のリストを取得するには?
- 26. カテゴリリストをマジェンタで取得するには?
- 27. クッキーの値を取得するには?
- 28. PowerBuilderでアクティブシートメニューを取得するには?
- 29. ボディータグのスタイルを取得するには
- 30. メールアドレスを取得するには?
D'oh! - ありがとう。それはドキュメントからこれを見るための簡単な方法がないことは残念です。 – Andy
パラメータのないコンストラクタはありません。 [this](http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.xmlnamespacemanager(v = vs.110).aspx)の記事を参照してください。 – Bernard
別の質問への回答を参照してください。 – MonkeyWrench