0

Can some one guide me how to parse this Xml type string ?

<data>
  <LastUpdate></LastUpdate>
  <AC1>12</AC1>
  <AC2>13</AC2>
  <AC3>14</AC3>
  <Moter></Moter>
  <Fan1></Fan1>
  <Fan2></Fan2>
  <TubeLight1></TubeLight1>
  <TubeLight2></TubeLight2>
  <Moter></Moter>
  <CloseAll></CloseAll>
</data>

I need to get all result in String or List or dictionary like AC1=12 , AC2=13 and so on

Thnaks in advance

2
  • 2
    like with an XDocument.Parse(xmlstring)? Commented Apr 11, 2011 at 17:10
  • any thing =) i dont mind. i just wana parse that Commented Apr 11, 2011 at 17:13

5 Answers 5

1

Use XDocument.Parse method:

string data = @"<data>
                  <LastUpdate></LastUpdate>
                  <AC1>12</AC1>
                  <AC2>13</AC2>
                  <AC3>14</AC3>
                  <Moter></Moter>
                  <Fan1></Fan1>
                  <Fan2></Fan2>
                  <TubeLight1></TubeLight1>
                  <TubeLight2></TubeLight2>
                  <Moter></Moter>
                  <CloseAll></CloseAll>
            </data>";

XDocument xmlDoc = XDocument.Parse(data);

var parsedData = from obj in xmlDoc.Descendants("data")
                 select new
                 {
                     LastUpdate = obj.Element("LastUpdate").Value,
                     AC1 = obj.Element("AC1").Value,
                     AC2 = obj.Element("AC1").Value,
                     ... and so on
                 }

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

0

This should work but you have to remove the duplicate Moter element from your XML - only then you can use a dictionary:

XDocument doc = XDocument.Load("test.xml");
var dictionary = doc.Descendants("data")
                    .Elements()
                    .ToDictionary(x => x.Name.ToString(), x => x.Value);
string ac1Value = dictionary["AC1"];

2 Comments

m geting Illegal characters in path. at line 'XDocument doc = XDocument.Load(xml);'
if you want to parse an XML string (and not a file) you have to use XDocument.Parse(xmlString)
0

If you wanted to go for Linq to XML then it would look something like:

        XElement root = XElement.Parse(s);
        Dictionary<XName, string> dict = root
            .Elements()
            .Select(x => new {key = x.Name, value = x.Value})
            .ToDictionary(x => x.key, x => x.value);

Just make sure you deal with duplicates the way you want.

Comments

0

I prefer using XLinq. Here is the sample (in VB.NET):

 Private Sub ParseIt()

        Dim xml = XElement.Parse(sampleXml)

        Dim dic As New Dictionary(Of String, String)

        For Each item In xml.Elements
            dic.Add(item.Name.LocalName, item.Value)
        Next

    End Sub

Also you can use it like this (I prefer this method):

Private Sub ParseIt()

    Dim xml = XElement.Parse("")

    Dim dic = (From item In xml.Elements).ToDictionary(Function(obj) obj.Name.LocalName, Function(obj) obj.Value)

End Sub

Comments

0

If you want to parse xml data string into 'Dataset' then you can use this sample

    string xmlString = @"/*.. .. .*/";

    DataSet data = new DataSet();

    data.ReadXml(new StringReader(xmlString));

Comments

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.