1

我有一个 XML 字符串,使用 C# 2.0,我必须读取该字符串并形成一个键值对或单独的列表。我必须使用下面的 XML 进行 Web 服务的字段映射。

下面是我的 xml 示例

<?xml version="1.0" encoding="utf-8" ?>
<Integration>
  <FiledMappings name ="Employee">
    <Field Name="EmployeeID">
      <DataSource>EmployeeNO</DataSource>
    </Field>
    <Field Name="Department">
      <DataSource>Department</DataSource>
    </Field>
    <Field Name="EmployeeName">
      <DataSource>Name</DataSource>
    </Field>
  </FiledMappings>
</Integration>
4

4 回答 4

3

试试这个代码;我使用DictionaryXmlDocument

var keyValues = new Dictionary<string, string>();

var document = new XmlDocument();
document.LoadXml(stringXml);
foreach (XmlNode node in document.SelectNodes(@"//Field"))
{
    keyValues.Add(node.Attributes["Name"].InnerText, 
                  node.InnerText);
}
于 2012-07-30T04:45:18.650 回答
1

您可以使用以下代码来获取所需的字典:

        StringBuilder s = new StringBuilder();
        s.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        s.AppendLine("<Integration>");
        s.AppendLine("<FiledMappings name =\"Employee\">");
        s.AppendLine("<Field Name=\"EmployeeID\">");
        s.AppendLine("<DataSource>EmployeeNO</DataSource>");
        s.AppendLine("</Field>");
        s.AppendLine("<Field Name=\"Department\">");
        s.AppendLine("<DataSource>Department</DataSource>");
        s.AppendLine("</Field>");
        s.AppendLine("<Field Name=\"EmployeeName\">");
        s.AppendLine("<DataSource>Name</DataSource>");
        s.AppendLine("</Field>");
        s.AppendLine("</FiledMappings>");
        s.AppendLine("</Integration>");

        Dictionary<string, string> d = new Dictionary<string, string>();

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(s.ToString());

        XmlNode x = doc.ChildNodes[1].ChildNodes[0];
        foreach (XmlNode n in x.ChildNodes)
            d[n.Attributes[0].Value] = n.FirstChild.FirstChild.Value;

        foreach (KeyValuePair<string, string> p in d)
            Console.WriteLine(string.Format("{0}:\t{1}", p.Key, p.Value));

        Console.ReadLine();

或者,如果似乎可以使用 .Net 3.5,您可以使用 Linq to xml,请参阅:

        StringBuilder s = new StringBuilder();
        s.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        s.AppendLine("<Integration>");
        s.AppendLine("<FiledMappings name =\"Employee\">");
        s.AppendLine("<Field Name=\"EmployeeID\">");
        s.AppendLine("<DataSource>EmployeeNO</DataSource>");
        s.AppendLine("</Field>");
        s.AppendLine("<Field Name=\"Department\">");
        s.AppendLine("<DataSource>Department</DataSource>");
        s.AppendLine("</Field>");
        s.AppendLine("<Field Name=\"EmployeeName\">");
        s.AppendLine("<DataSource>Name</DataSource>");
        s.AppendLine("</Field>");
        s.AppendLine("</FiledMappings>");
        s.AppendLine("</Integration>");

        XElement x = XElement.Parse(s.ToString());

        Dictionary<string, string> d = x.Element("FiledMappings").Elements("Field").ToDictionary(e => e.Attribute("Name").Value, e => e.Element("DataSource").Value);
        foreach (KeyValuePair<string, string> p in d)
            Console.WriteLine(string.Format("{0}:\t{1}", p.Key, p.Value));

        Console.ReadLine();
于 2012-07-30T02:26:28.653 回答
0

这就是我能够动态地做的事情:

private Dictionary<string, string> Load_XML_wsMapping(String _WSMapping)
        {
            // Web Service Mapping forms Key Value Pair
            XmlDocument doc = new XmlDocument(_WSMapping);
            doc.LoadXml();
            Dictionary<string, string> dictXMLMapping = new Dictionary<string, string>();

            try
            {
                XmlNodeList list = doc.FirstChild.NextSibling.FirstChild.ChildNodes;

                for (int i = 0; i < list.Count; i++)
                {
                    XmlElement el = ((System.Xml.XmlElement)(list[i]));
                    dictXMLMapping.Add(el.Attributes[0].Value, list[i].InnerText);

                }
            }
            catch (Exception err)
            {

                throw new Exception("Error occurred while mapping Web Service -- Bad XML." + err.Message);
            }
            return dictXMLMapping ;
        }
于 2012-07-31T17:20:01.023 回答
-1

您可以在下面使用字典

 private static IDictionary<string, string> parseReplicateBlock(StreamReader reader)
于 2012-07-30T01:38:05.320 回答