c#xml - 如果项目名称相同,则仅显示一个项目/节点

时间:2012-07-31 10:29:13

标签: c# xml for-loop foreach

这是.NET 2.0项目。首先是xml我要解析的内容:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <diagramer>
    <PKTABLE_NAME>TECH_Y_Category</PKTABLE_NAME>
    <FKTABLE_NAME>TECH_File</FKTABLE_NAME>
  </diagramer>
  <diagramer>
    <PKTABLE_NAME>TECH_File</PKTABLE_NAME>
    <FKTABLE_NAME>TECH_Version</FKTABLE_NAME>
  </diagramer>
  <diagramer>
    <PKTABLE_NAME>TECH_Group</PKTABLE_NAME>
    <FKTABLE_NAME>TECH_D_Group_File_Version</FKTABLE_NAME>
  </diagramer>
  <diagramer>
    <PKTABLE_NAME>TECH_Layout</PKTABLE_NAME>
    <FKTABLE_NAME>TECH_File</FKTABLE_NAME>
  </diagramer>
  <diagramer>
    <PKTABLE_NAME>CK_List</PKTABLE_NAME>
    <FKTABLE_NAME>CK_Vote</FKTABLE_NAME>
  </diagramer>
  <diagramer>
    <PKTABLE_NAME>CK_List</PKTABLE_NAME>
    <FKTABLE_NAME>CK_Score</FKTABLE_NAME>
  </diagramer>
  <diagramer>
    <PKTABLE_NAME>TECH_Page</PKTABLE_NAME>
    <FKTABLE_NAME>TECH_Page</FKTABLE_NAME>
  </diagramer>
  <diagramer>
    <PKTABLE_NAME>CK_List</PKTABLE_NAME>
    <FKTABLE_NAME>Comments</FKTABLE_NAME>
  </diagramer>
</DocumentElement>

正如您所看到的,某些PKTABLE_NAME值相同。 我使用下面的代码循环遍历该xml:

StringBuilder sb = new StringBuilder();
foreach (XmlNode items in diagramTables)
{
  string pkTable = items["PKTABLE_NAME"].InnerText.Replace("_",@"\_");
  string fkTable = items["FKTABLE_NAME"].InnerText.Replace("_",@"\_");

  sb.Append(pkTable);
  sb.Append(" got ");
  sb.Append( fkTable + Environment.NewLine);

}

这不是我想要实现的目标。我希望拥有所有价值但只有一次(我不想重复它们)。毕竟我想从上面的xml中写出像这样的东西:

  • TECH_Y_Category TECH_File
  • TECH_File TECH_Version
  • TECH_Group TECH_D_Group_File_Version
  • TECH_Layout TECH_File
  • CK_List CK_Vote | CK_Score |评论
  • TECH_Page TECH_Page

我希望你明白我想要实现的目标。

EDIT。

Morawski的代码对我来说很完美,所以问题解决了;)。

2 个答案:

答案 0 :(得分:1)

类似的东西:

Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
foreach (XmlNode items in diagramTables)
{
  string pkTable = items["PKTABLE_NAME"].InnerText.Replace("_",@"\_");
  string fkTable = items["FKTABLE_NAME"].InnerText.Replace("_",@"\_");
  if (!dict.ContainsKey(pkTable))
  {
    dict.Add(pkTable, new List<string>());
  }
  if (!dict[pkTable].Contains(fkTable))
  {
      dict[pkTable].Add(fkTable);
  }
}
sb.Append("========================================================="); // making it easier for you
foreach(KeyValuePair<string, List<string>> kvp in dict)
{
  sb.Append(kvp.Key);
  sb.Append(" got ");
  sb.AppendLine(String.Join("|", kvp.Value.ToArray()));
}
sb.Append("========================================================="); // making it easier for you

答案 1 :(得分:0)

将结果存储在DictionaryList中:

var nodes = new Dictionary<string, List<string>>();

XmlDocument document = new XmlDocument();

foreach (XmlNode childNode in document.ChildNodes)
{
    if(nodes.ContainsKey(childNode.Name))
        nodes[childNode.Name].Add(childNode.InnerText);
    else
        nodes.Add(childNode.Name, new List<string> {childNode.InnerText});
}

// and printing

foreach (var node in nodes)
{
    string values = node.Value.Aggregate("", (current, s) => current + ("|" + s));
    Console.WriteLine(node.Key + " gots " + values);
}