【问题标题】:Add nodes in XML based on Xpath and also add multiple similar tags in same parent基于 Xpath 在 XML 中添加节点,并在同一个父节点中添加多个相似的标签
【发布时间】:2013-03-13 18:53:57
【问题描述】:

我拥有构建 XML 所需的所有 xpath。所以我需要用 xpaths 和相关的值来构建这个 XML。但在某些情况下,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>

你可以看到标签&lt;Official&gt;o&lt;/Official&gt;&lt;Official&gt;r&lt;/Official&gt;重复标签不同的innerText[与&lt;Sex&gt;&lt;Field1&gt;m&lt;/Field1&gt;&lt;Field1&gt;f&lt;/Field1&gt;&lt;/Sex&gt;相同]。但是当我尝试创建这样的 XML 时,输出如下:

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

&lt;Official&gt;o, r&lt;/Official&gt;

以下是我用来基于 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;
    }

那么我如何创建单独的标签而不是用逗号分隔的内部文本的单个标签。

编辑我

我发现了问题,我将 xpath 和相应的值作为查询字符串发送到此页面。现在对于我的情况,如果存在多个相同的标签,Request.QueryString 会产生逗号分隔的值[例如:m,f]。

好吧,我尝试向节点添加一个属性,但这只添加了逗号分隔数据中的最后一个数据。

任何修复???就像我应该如何调用该函数来添加具有一些属性的节点,这些属性使相同的 xpath 节点成为不同的节点。 提前致谢。

【问题讨论】:

    标签: c# xml xml-serialization


    【解决方案1】:

    据我了解您的代码,当您想创建第二个 Official 节点时,makeXPath 函数将在第一个 for 循环中找到现有的 Official 节点。 node = doc.SelectSingleNode(subXpath) 将返回此现有节点,然后您将设置 innerText 而不创建新节点。 我相信您应该在第一个 for 循环之前检查完整路径的存在,然后创建兄弟姐妹。

    类似

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2017-01-23
      • 1970-01-01
      相关资源
      最近更新 更多