1

这是 .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

我希望你明白我想要达到的目标。

编辑。

来自 Morawski 的代码对我来说非常有效,所以问题得到了解决;)。

4

2 回答 2

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
于 2012-07-31T10:37:46.450 回答
0

将结果存储在 aDictionaryList

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);
}
于 2012-07-31T10:38:27.983 回答