Hi I am developing an application in Silverlight 3.0, I want to make a generic parser of XML in it, as on every call to webservice I am receiving a different XML, I want to make it generic so that I receive an XML in native datastructure of C#? Please help me out? like I am getting XML like this one time
<test>
<node1></node1>
<node2></node2>
</test>
and other time
<mytest>
<application name="XYZ">My Application</application>
<application name="ABC">My Application</application>
</mytest>
I want the generic parser, e.g. it makes some tree structure of whole XML
From stackoverflow
-
You can use .NET xml serialization.
Given the xml that looks like this
<TestObject> <FirstProperty>SomeValue</FirstProperty> <SecondProperty>17</SecondProperty> </TestObject>
C# object
[Serializable] public class TestObject { public string FirstProperty { get; set; } public int SecondProperty { get; set; } }
Here is the code to convert the xml to the object
string xml = @"<TestObject> <FirstProperty>SomeValue</FirstProperty> <SecondProperty>17</SecondProperty> </TestObject>"; XmlSerializer serializer = new XmlSerializer(typeof(TestObject)); using (StringReader reader = new StringReader(xml)) { using (XmlTextReader xmlReader = new XmlTextReader(reader)) { TestObject obj = serializer.Deserialize(xmlReader) as TestObject; } }
Robert Giesecke : Just to prevent misunderstandings: You do not need the Serialize attribute to use the XmlSerializer. Has no effect for this kind of serialization.Ummar : but for this type of parser I need to make Serializeable object for each XML, e.g if I had 100 type of XMLs, then I need 100 objects... This is not feasible for me. I want just to store them in a Generic structure, like List or Dictionary. -
I have found solution Generic XML parser works for me.
0 comments:
Post a Comment