2012-07-30 109 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 ; 
     }