Reputation: 3625
I have a XML String, Using C# 2.0, I have to read that string and form a key value pair or separate lists. I have to use below XML for field mapping for Web Service.
below is my sample of my 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>
Upvotes: 1
Views: 979
Reputation: 3625
this is what I was able to do dynamically :
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 ; }
Upvotes: 0
Reputation: 10357
Try this code; I used Dictionary
and XmlDocument
:
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);
}
Upvotes: 3
Reputation: 18563
You could use the following code to get the required dictionary :
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();
Or if it may appear to be possible to use .Net 3.5, you could utilize Linq to xml, please, see:
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();
Upvotes: 1
Reputation: 1900
You could use a disctionary for example below
private static IDictionary<string, string> parseReplicateBlock(StreamReader reader)
Upvotes: -1