简体   繁体   English

根据Xpath在XML中添加节点,并在同一父级中添加多个相似标签

[英]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. 我拥有构建XML所需的所有必需的xpath。 So I need to build this XML with the xpaths and associated values. 因此,我需要使用xpaths和关联值来构建此XML。 But there are situations where the XML might need to have same tags in same parent as following: 但是在某些情况下,XML可能需要在同一父级中具有相同的标签,如下所示:

<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> ]. 您可以看到标签<Official>o</Official><Official>r</Official>重复的标签具有不同的innerText [与<Sex><Field1>m</Field1><Field1>f</Field1></Sex> ]。 But when I try to create such an XML the output is as following: 但是,当我尝试创建这样的XML时,输出如下:

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

AND <Official>o, r</Official> AND <Official>o, r</Official>

The following is the code that I used to create the nodes based on the xpaths: 以下是我用于基于xpath创建节点的代码:

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. 因此,如何创建单独的标签,而不是用逗号分隔的innerText创建单个标签。

EDIT I 编辑我

I found the issue, I was sending the xpaths and corresponding values to this page as query string. 我发现了问题,我正在将xpaths和相应的值作为查询字符串发送到此页面。 Now for my case, where there exists more than one same tag, the Request.QueryString yields comma separated values [eg: m, f]. 现在,对于我来说,如果存在多个相同的标记,则Request.QueryString会产生逗号分隔的值[例如: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. 就像我应该如何调用该函数来添加具有某些属性的节点,这些属性使相同的xpath节点成为不同的节点。 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. 据我了解您的代码,当您要创建第二个Official节点时,makeXPath函数将在第一个for循环中找到一个现有的Official节点。 node = doc.SelectSingleNode(subXpath) will return this existing node, and you will then set the innerText without creating a new one. node = doc.SelectSingleNode(subXpath)将返回此现有节点,然后在不创建新节点的情况下设置innerText I believe you should check for the existence of the full path before the first for loop and create the sibling then. 我相信您应该在第一个for循环之前检查完整路径的存在,然后创建同级。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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