使用C#2.0读取XML字符串

时间:2012-07-30 01:11:50

标签: c# xml

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

下面的

是我的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 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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到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();

答案 2 :(得分: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 ;
        }

答案 3 :(得分:-1)

您可以使用下面的词典

 private static IDictionary<string, string> parseReplicateBlock(StreamReader reader)