कैसे प्राप्त करूं मैं XElement.XPathSelectElements() अधिभार को कॉल करने का प्रयास कर रहा हूं जिसके लिए IXmlNamespaceResolver ऑब्जेक्ट की आवश्यकता है। क्या कोई मुझे दिखा सकता है कि IXmlNamespaceResolver कैसे प्राप्त करें (या बनाना)? मैंमैं IXmlNamespaceResolver
उत्तर
आप एक XmlNamespaceManager कि कि इंटरफेस
उपयोग निर्माता है जो एक XmlNameTable
लेता है, new NameTable()
के माध्यम से System.Xml.NameTable
का एक उदाहरण में गुजर लागू करता है का उपयोग कर सकते अपनी क्वेरी में नामस्थान मैं उपयोग करना चाहते हैं की एक सूची है। इसके बाद आप नामस्थान जोड़ने के लिए 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())
।
उदाहरण के लिए, यदि आप एक XML दस्तावेज़ की तरह
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>");
नामस्थान का उपयोग करता है तो आप Grade
नोड्स
var namespaceResolver = new XmlNamespaceManager(new NameTable());
namespaceResolver.AddNamespace("math", "http://www.ludlowcloud.com/Math");
var grades = xDoc.XPathSelectElements("//math:Student/math:Grade", namespaceResolver);
करके जबकि कैसे उपयोग करने के लिए पर खोज कर मैं इस पोस्ट पाया प्राप्त कर सकते हैं एक एसओएपी प्रतिक्रिया को संसाधित करने के लिए [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");
नाम मेरे मामले में मैं इस तरह के एक दस्तावेज है मैं उपसर्गों के लिए उपयोग करता हूं ("एसओएपीएनएस" और "नोफफेंस") मनमाने ढंग से हैं। एक ऐसे नाम का प्रयोग करें जो एक पठनीय कोड के लिए सुविधाजनक है।
[लिफाफा] तत्व का चयन करने के लिए, मैं का उपयोग करें:
var theDetails = theDocumentXDoc.XPathSelectElement("//theSoapNS:Envelope/theSoapNS:Body/noSuffNS:queryResponse/noSuffNS:response/noSuffNS:theDetails", theNamespaceIndicator);
:
var theQueryResponse = theDocumentXDoc.XPathSelectElement("//theSoapNS:Envelope/theSoapNS:Body/noSuffNS:queryResponse", theNamespaceIndicator);
[theDetails] में सभी विवरण देखने के लिए:
var theEnvelope = theDocumentXDoc.XPathSelectElement("//theSoapNS:Envelope", theNamespaceIndicator);
[queryResponse] तत्व का चयन करने के लिए
यहाँ एक उदाहरण कार्यक्रम है (सी # 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
धन्यवाद! यह वही है जो मैं यहाँ आया था। –
डी ओह! -धन्यवाद। यह शर्म की बात है वहाँ प्रलेखन – Andy
कोई parameterless निर्माता है से देखने के लिए एक आसान तरीका नहीं है है। देखें [इस] (http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.xmlnamespacemanager (v = vs.110) .aspx) लेख। – Bernard
इस उत्तर को दूसरे प्रश्न पर देखें। – MonkeyWrench