简体   繁体   中英

Add nodes in XML based on Xpath and also add multiple similar tags in same parent

I have all the necessary xpaths to build a XML. So I need to build this XML with the xpaths and associated values. But there are situations where the XML might need to have same tags in same parent as following:

<root>
  <Name>
    <Address>
      <Fname>bbb</Fname>
      <Lname>bbb</Lname>
      <Official>o</Official>
      <Official>r</Official>
      <District>Ekm Edited</District>
      <State>Kerala Edited</State>
    </Address>
    <Sex>
      <Field1>m</Field1>
      <Field1>f</Field1>
    </Sex>
    <Qualification>
      <EDUCATION>10</EDUCATION>
    </Qualification>
  </Name>
</root>

You can see tags <Official>o</Official> and <Official>r</Official> repeated tag with different innerText[The same with <Sex><Field1>m</Field1><Field1>f</Field1></Sex> ]. But when I try to create such an XML the output is as following:

<Sex>
  <Field1>m, f</Field1>
</Sex>

AND <Official>o, r</Official>

The following is the code that I used to create the nodes based on the xpaths:

public XmlNode makeXPath(XmlDocument doc, string xpath, string innertext)
    {
        string[] partsOfXPath = xpath.Split('/');
        XmlNode node = null;
        for (int xpathPos = partsOfXPath.Length; xpathPos > 0; xpathPos--)
        {
            string subXpath = string.Join("/", partsOfXPath, 0, xpathPos);
            node = doc.SelectSingleNode(subXpath);
            if (node != null)
            {
                // append new descendants
                for (int newXpathPos = xpathPos; newXpathPos < partsOfXPath.Length; newXpathPos++)
                {
                    node = node.AppendChild(doc.CreateElement(partsOfXPath[newXpathPos]));

                }
                break;
            }
        }
        node.InnerText = innertext.TrimStart(' ');
        return node;
    }

So how do i create seperate tags rather than a single tag with comma separated innerText.

EDIT I

I found the issue, I was sending the xpaths and corresponding values to this page as query string. Now for my case, where there exists more than one same tag, the Request.QueryString yields comma separated values [eg: m, f].

Well I tried to add a attribute to the nodes but that only adds the last data in the comma separated data.

Any fixes??? Like how should I call the function to add nodes with some attributes that makes the same xpath nodes as different nodes. Thanks in advance.

As I understand your code, when you want to create the second Official node, the makeXPath function will find an existing Official node in the first for loop. node = doc.SelectSingleNode(subXpath) will return this existing node, and you will then set the innerText without creating a new one. I believe you should check for the existence of the full path before the first for loop and create the sibling then.

Something like

public XmlNode makeXPath(XmlDocument doc, string xpath, string innertext)
    {
        string[] partsOfXPath = xpath.Split('/');
        XmlNode node = null;
        if (doc.SelectSingleNode(xpath) != null) {
            //get the parent
            node = doc.SelectSingleNode(string.Join("/", partsOfXPath, 0, partsOfXPath.Length-1));
            node = node.AppendChild(doc.CreateElement(xpath));
            node.InnerText = innertext.TrimStart(' ');
        }
        else {
            for (int xpathPos = partsOfXPath.Length; xpathPos > 0; xpathPos--)
            {
                string subXpath = string.Join("/", partsOfXPath, 0, xpathPos);
                node = doc.SelectSingleNode(subXpath);
                if (node != null)
                {
                    // append new descendants
                    for (int newXpathPos = xpathPos; newXpathPos < partsOfXPath.Length; newXpathPos++)
                    {
                        node = node.AppendChild(doc.CreateElement(partsOfXPath[newXpathPos]));

                    }
                    break;
                }
            }
            node.InnerText = innertext.TrimStart(' ');
        }
        return node;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM