0

I am new to xml , so please don't mind if it is too trivial question

Assume I have a xml file like below

 <Person>       
        <Name>John-Jaime-Winston Junior</Name>       
    </Person>
    <Person>       
        <Name>Steve</Name>
    </person   

Now i will have a person object , Can i know how to read the xml and cover to a array of objects.

Finally i want something like a list which will have all person objects.

I am unable to get the start how to do this as i am new to xml

class person {

 string _name

 public string Name
        {
            get { return _name}
            set { _name= value; }
        }

}

Thanks in advance

3
  • Is that really the XML you're using? Commented Dec 14, 2010 at 19:47
  • @chaos pandioin , yes i am using xml , i just a sample code as i cannot post my entire xml , its almost the same concept Commented Dec 14, 2010 at 19:51
  • FYI: The sample is not valid XML, because XML requires there to be a single root node. Suggest changing it to something like ChaosPandion's example. Commented Dec 14, 2010 at 20:10

2 Answers 2

3

Given .NET 3.5 and System.Xml.Linq this is quite easy.

var q = from e in XElement.Parse(xml).Elements()
        select new Person() { 
            Name = e.Element("Name").Value 
        };

var p = q.ToList();

You will need to provide valid XML like what follows:

<People>
    <Person>
        <Name>Jim</Name>
    </Person>
    <Person>
        <Name>Bill</Name>
    </Person>
</People>
Sign up to request clarification or add additional context in comments.

Comments

1

I think you want something like XmlSerializer, you can serialize and deserialize objects by this. just should define properties public

[Serializable()]
public class person 
{

        string _name

        public string Name
        {
            get { return _name}
            set { _name= value; }
        }

}

and use it:

        XmlSerializer serializer = new XmlSerializer(typeof(Person));
        StreamWriter sw = new StreamWriter("c:\\out.xml");
        serializer.Serialize(sw,new Person{Name = "Test"});
        sw.Close();


        StreamReader sr = new StreamReader("c:\\out.xml");
        var outVal = serializer.Deserialize(sr) as Person;

But for parsing xml in normal way you can use XDocument or XPath, ...

3 Comments

this is what i want , so that i can write one method which can pass any type of object
i was looking something like above and a generic method which takes the person and automatically converts to object , i achieved it somehow , if you have better code please post it.
@gov, you should read XmlSerializre link I left for you, serialization of object is a normal way of serialization, you can also do it with soap formatter, binaryformatter and DataContractSerializer, for example in remoting or WCF objects should be serialized to send and in receiver they should be deserialize, and this can achieve by this way, say what exactly you want, if I know I'll help you :)

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.