XmlNode.SelectNodes basics?

I am not sure why this is not working.

I have an XmlNode in a known format. It:

<[setting-name]>
    <dictionary>
       <[block-of-xml-to-process]/>
       <[block-of-xml-to-process]/>
       <[block-of-xml-to-process]/>
    </dictionary>
</[setting-name]>

I have a link to node in a variable called pattern . I want an iterable set of nodes, each of which is represented [block-of-xml-to-process] above. The name and structure of the blocks are currently unknown. The name [Setting-name] is known.

It seems pretty simple. I can imagine half a dozen XPATH expressions that should point to blocks. I tried:

XmlNodeList kvpsList = pattern.SelectNodes(String.Format(@"/{0}/dictionary/*", _CollectionName));
XmlNodeList kvpsList = pattern.SelectNodes(String.Format(@"{0}/dictionary/*", _CollectionName));
XmlNodeList kvpsList = pattern.SelectNodes(@"//dictionary/*");
XmlNodeList kvpsList = pattern.SelectNodes(@"//dictionary");

But apparently I lack a basic understanding of XPATH or some special .SelectNodes trick, because none of them work in sequence.

What am I doing wrong?

+3
6

"@" XPath?

XmlNodeList kvpsList = pattern.SelectNodes("//dictionary");

- : -)

+3

:

XmlNodeList kvpsList = pattern.SelectNodes(@"//dictionary:child");

XmlNodeList kvpsList = pattern.SelectNodes(@"/[setting-name]/dictionary:child");

"" , ?

+2

pattern?
DOM XML?

, pattern.SelectNodes("//dictionary/").ChildNodes.Count

EDIT: xml?

0

? , "pattern.OuterXml", , .

0

, , :

XmlNodeList kvpsList = pattern.SelectNodes("dictionary");

kvpsList [block-of-xml-to-process] -s, , . = ')

0

, , . . Xml-SelectNodes XmlNamespaceManager

, , XmlNamespaceManager XmlDocument NameTable, , "a", NamespaceURI . , XmlDocument NamespaceURI , DocumentElement NameSpaceURI . , .

XmlDocument doc = new XmlDocument();
doc.Load( file.FullName );
XmlNode docElement = doc.DocumentElement as XmlNode;
XmlNamespaceManager nsman = new XmlNamespaceManager( doc.NameTable );
nsman.AddNamespace( "a", docElement.NamespaceURI );
docElement.SelectNodes( "a:wavetrack", nsman ); //docElement.SelectNodes("wavetrack") wasn't working
0

Source: https://habr.com/ru/post/1705602/


All Articles