2012-07-30 90 views
1

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

下面

是我的我的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> 
+0

如果你的O \ P个字典是这样的 '{ “雇员“:”“EmployeeNO”, “Department”:“Department”, ..... }'?? – 2012-07-30 02:27:30

回答

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); 
} 
+0

我正在寻找一些dyanamic ..因为名称可能会改变XML .. – msbyuva 2012-07-31 17:15:40

+0

感谢您的答案,它的工作原理但万一如果名称更改为XML,那么它可能无法识别@ //字段我发布了我的答案,下面我使用了XML Nodelist属性.. – msbyuva 2012-07-31 17:51:01

+0

@msbyuva:我的代码只是一个示例代码。你必须改变你的问题。 – Ria 2012-08-01 01:15:29

-1

你可以使用一个disctionary例如下面

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

什么是'parseReplicateBlock'? – 2012-07-30 02:07:24

+0

tammy我无法理解你在说什么.. – msbyuva 2012-07-31 17:51:55

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(); 
+0

显然,我的例子没有有效性检查,并且适用于输入xml字符串结构严格遵循msbyuva – horgh 2012-07-30 02:31:10

+1

给出的示例,如果您构建的字符串已满的常量,只需添加(连接)它们,它将被编译器合并为单个字符串。通过将它们扔到一个字符串构建器中,您不会获得任何东西,并且可读性和一些性能都很低。更好的是,如果您希望它跨越多行,请使用逐字字符串。 – 2012-07-30 05:06:44

+0

我正在寻找一些dyanamic ..因为名称可能会改变XML ..我必须使用.NET 2.0 – msbyuva 2012-07-31 17:16:24

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 ; 
     }