taher chhabrawala
taher chhabrawala

Reputation: 4130

Extract values from string containing xml in silverlight

string xmlText= "
                  <Person>
                     <Name>abc</Name>
                     <Age>22</Age>
                  </person>";

i want the values abc and 22 m using silverlight 4 and XmlTextReader is not available.

Upvotes: 2

Views: 1982

Answers (1)

Babak
Babak

Reputation: 5338

You can parse XML data in Silverlight by using either LINQ to XML or XmlReader.

Here's some example code that uses XmlReader. It's very simple and only works with the exact input string that you defined. But, it should be enough to get you on your way.

string xmlText= @"
                  <Person>
                    <Name>abc</Name>
                      <Age>22</Age>
                  </Person>";
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlText)))
{
  // Parse the file and display each of the nodes.
  while (reader.Read())
  {
    if (reader.Name == "Name" && reader.NodeType == XmlNodeType.Element)
    {
      // Advace to the element's text node
      reader.Read();
      // ... do what you want (you can get the value with reader.Value)
    }
    else if (reader.Name == "Age" && reader.NodeType == XmlNodeType.Element)
    {
      // Advace to the element's text node
      reader.Read();
      // ... do what you want (you can get the value with reader.Value)
    }
}

Here's an article with more details:

http://msdn.microsoft.com/en-us/library/cc188996(VS.95).aspx

Upvotes: 1

Related Questions