【问题标题】:How to merge repeating node values into one node?如何将重复的节点值合并到一个节点中?
【发布时间】:2020-01-21 21:03:02
【问题描述】:

我有以下 XML:

<root>
    <table>
        <items>Item 1</items>
    </table>
    <table>
        <items>Item 2</items>
    </table>
    <table>
        <items>Item 3</items>
    </table>
    <table>
        <items>Item 4</items>
    </table>
</root>

我想将所有项目值合并到一个节点中,以便可以在某些代码中使用该节点值。

最终结果应该是这样的:

<root>
    <table>
        <items>Item 1, Item 2, Item 3, Item 4</items>
    </table>
</root>

最好的方法是什么?

【问题讨论】:

  • 您已经标记了c#linq,但这里没有这样的代码。有什么尝试?先着眼“一条路”;以后可以朝着“最好的方式”努力。
  • 我怀疑最好的方法是在代码中而不是在 XML 中组合值。如果您要更改 XML,那么您将丢失信息。
  • 嗯,我是新手,这是我必须使用的 XML。需要一个节点中的所有值,并且需要为此使用 C#,然后我才能在流程的进一步步骤中使用该信息
  • 您是否需要新 XML 文件中一个节点中的所有值(请注意,添加逗号和空格会使它们成为不同的值)还是将这些值包含在一个 List,或者您希望它们在一个没有所有周围 XML 的字符串变量中?
  • 我需要将我当前的 xml(参见第一个代码块)转换为一个新的 XML 文件,该文件看起来像我发布的第二个代码块。新的 XML 将成为流程的输入

标签: c# xml linq linq-to-xml


【解决方案1】:

试试:

     var newXml = new XDocument(
                    new XElement("root",
                       new XElement("table",
                          new XElement("items", 
        XDocument.Parse(xml)
           .Descendants()
           .SelectMany(x => x.Elements("table"))
           .Select(x => x.Element("items").Value)
           .Aggregate((x, y) => string.Join(", ", x, y))))));

【讨论】:

  • 我在家的时候试试这个 tnx 等待您的回复
【解决方案2】:

感谢您的帮助,尝试了多种解决方案。

我最终得到了以下解决方案:

    var input = "XML";

    XDocument doc = XDocument.Parse(input);

    XElement root = doc.Root;
    XElement parentTable = root.Element("parentTable");
    parentTable.Add(new XElement("components"));
    XElement components = parentTable.Element("components");
    ArrayList componentArray = new ArrayList();

    foreach (var d in doc.Descendants("table"))

    {
        componentArray.Add(d.Value + ", ");
    }

    components.Add(componentArray);

    doc.Save("XML");

【讨论】:

    【解决方案3】:

    使用 Xml Linq:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
                XElement root = doc.Root;
    
                List<XElement> children = root.Elements().ToList();
                string[] items = root.Descendants("items").Select(x => (string)x).ToArray();
    
                for (int i = 0; i < children.Count; i++)
                {
                    if (i == 0)
                    {
                        children[i].Element("items").ReplaceWith(new XElement("items", string.Join(",", items)));
                    }
                    else
                    {
                        children[i].Remove();
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多