XML MSXML2.DOMDocument30 selectNodes

Dear all,
I have following xml document:

<Site some_attributes="xxx">
    <Page id="1"></Page>
    <page id="2"></page>
    <Style></Style>
</Site>

Open in new window

Need to use following C# code to select Site node[only] with its all attributes + Page id=2 [with its all child nodes].

MSXML2.DOMDocument30 XMLSourceDataSingleDoc = new MSXML2.DOMDocument30();                                   
XMLSourceDataSingleDoc.loadXML(XMLSiteSourceData.xml );
XMLSourceDataSingleDoc.selectNodes("xxxxxxxxxxx");

Open in new window

ethar1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
You will have to give XPATH correctly, I haven't tried using MSXML but it should be something like below.
/Site[Page[@id = 1]]. // node that you want to select and the filters within that node.
I tried using C# XMLDocument and SelectSingle node methods and it looks like below.
            XmlDocument doc = new XmlDocument();
            doc.Load(@"E:\Test.xml");
            XmlNode root = doc.DocumentElement;
            // Add the namespace.
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("st", "urn:sites-schema");
            XmlNode node = root.SelectSingleNode(
    "descendant::st:Site[st:Page[@id='1']]", nsmgr);
            Console.WriteLine(node.InnerXml);

Open in new window

XML looks like below:
<?xml version='1.0'?>
<sites xmlns="urn:sites-schema">
  <Site some_attributes="xxx">
    <Page id="1"></Page>
    <Page id="2"></Page>
    <Style></Style>
  </Site>
</sites>

Open in new window


You look at below reference for more help:
https://msdn.microsoft.com/en-us/library/cc189056%28v=vs.95%29.aspx
http://www.csharp-examples.net/xml-nodes-by-attribute-value/
ethar1Author Commented:
Thanks for your reply,
the output I need is :
<Site some_attributes="xxx">
    <page id="1"></page>
</Site>

Open in new window

Karrtik IyerSoftware ArchitectCommented:
You can use the below XPath:
   XmlNode node = root.SelectSingleNode(
    "descendant::st:Site/st:Page[@id='2']", nsmgr);
And get the outerXML
node.OuterXml
which shall give you
<Page id="2"></Page>
Then you may have to append the parent node's XML to get the desired value.
I shall check further if there is any other direct way to just the output that you desire (as per your last comment)
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Karrtik IyerSoftware ArchitectCommented:
I think the best option for you is to apply an XSLT transformation to convert XML in one format to another XML.
You can use XSLT transformation in C#, see reference below.
https://support.microsoft.com/en-us/kb/307322

I did the below steps:
Went to:
1> http://www.freeformatter.com/xsl-transformer.html
2> Entered your input XML as:
<Site some_attributes="xxx">
    <Page id="1"></Page>
    <Page id="2"></Page>
    <Style></Style>
</Site>

Open in new window

3> Entered the XSL as : (basically this XSL copies the source XML with some filtering criteria, to include only page with ID 1)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

<xsl:template match="Page[@id != '1']"/>
<xsl:template match="Style[normalize-space(response) = '']"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Open in new window

4> Clicked Transform XML and that gave me below result:
<?xml version="1.0" encoding="UTF-8"?>
<Site some_attributes="xxx">
   <Page id="1" />
</Site>

Open in new window

Perform the same steps in C# code via the example shown in the below MSDN link:
https://support.microsoft.com/en-us/kb/307322
Hope this helps.

Thanks,
Karrtik

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ethar1Author Commented:
Thanks for your great efforts...
Karrtik IyerSoftware ArchitectCommented:
You are welcome!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.