1

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>
1
  • Should your o\p dictionary be something like this { "EmployeeID":""EmployeeNO", "Department":"Department", ..... } ?? Commented Jul 30, 2012 at 2:27

4 Answers 4

3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, it works but in case if names changes in XML then it might not recognize the @//Field I posted my answer below which I have used XML Nodelist properties..
@msbyuva: my code just was a sample code. you must change it for your issues.
1

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

3 Comments

Obviously, my example has no validity checks and applies that the input xml string structure strictly follows the example given by msbyuva
FYI, if you're building up a string full of constants, just add (concatenate) them, it will be merged into a single string by the compiler. By throwing them in a string builder, you gain nothing and loose readability and some performance. Better yet, use a verbatim literal string if you want it to span multiple lines.
I am looking for something dyanamic.. as names may change in XML.. and I have to use .NET 2.0
0

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

Comments

-1

You could use a disctionary for example below

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

1 Comment

What is parseReplicateBlock?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.