0

I have a number of XPaths from which I'd like to create an XML document, probably using the XmlDocument class and preferably utilising some existing functionality rather than building node-by-node in some kind of possibly recursive loop.

So given the 3 xpaths:

THIS/IS/FIRST/XPATH
THIS/IS/FIRST/XPATH/GOING/DEEPER
THIS/IS/SECOND/XPATH

I would like to produce:

<THIS>
  <IS>
    <FIRST>
      <XPATH>
        <GOING>
          <DEEPER>
          </DEEPER>
        </GOING>
      </XPATH>
    </FIRST>
    <SECOND>
      <XPATH>
      </XPATH>
    </SECOND>
  </IS>
</THIS>

I'm hoping the code is something simply along the lines of this, with XPaths being added in any order:

var doc = new XmlDocument();
doc.AddXPath("THIS/IS/FIRST/XPATH");
doc.AddXPath("THIS/IS/SECOND/XPATH");
doc.AddXPath("THIS/IS/FIRST/XPATH/GOING/DEEPER");
string result = doc.ToString();`

Many thanks!

webhead
  • 1
  • 2

1 Answers1

0

XPath purpose is to query xml documents, not to create them, so I don't think what you are trying to achieve is possible this way.

Maybe you can get inspiration there :

Create XML Nodes based on XPath?

B. Lec
  • 312
  • 2
  • 13
  • Post by Eranga Dissanayaka did exactly what I want, with repeated calls to GenerateXPathXmlElements(). Thanks to you both. – webhead Mar 08 '19 at 16:06