XML parsing - how?

Hi

I have this XML file:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<response status=\"ok\">
   <device id=\"iPhone\">
      <name>Test</name>
      <year>2014</year>
      <files count=\"2\">
         <file id=\"01\" name=\"File1\" username=\"bond\" />
         <file id=\"02\" name=\"File2\" username=\"james\" />
      </files>
   </device>
</response>

Open in new window


and I want to parse it into this class:

        public class device
        {
            public string name { get; set; }
            public int year { get; set; }
            public List<files> filesList { get; set; }

            public class files
            {
               public string id { get; set; }
               public string name { get; set; }
               public string username { get; set; }
            }

         }

Open in new window


What's the fastest way to do this? (c#)

Thanks

James
RuttensoftAsked:
Who is Participating?
 
Deepak SubburajanDB DeveloperCommented:
I am not sure about having list in Class on parsing XML. I have worked on this before with Array of Class Objects.

Can you try something like this?

 
   [XmlRoot()]
    public class Device
    {
        public string name;
        public int year;
        private Files[] files;

        public Files[] files {
            get { return files; }
            set { files = value; }
        }
    }

    public class Files
    {
        [XmlAttribute]
        public string id {get; set;}
        public string name {get; set;}
        public string username {get; set;}
    }

XmlSerializer serializer = new XmlSerializer(typeof(Device));     
Device dv = (Device)serializer.Deserialize(new StringReader(xml));
                                                                  
Console.WriteLine(dv.name);                                       
foreach(Files e in dv.Files) {                                    
    Console.WriteLine(e.Id);                                      
}                                                                 
                                                                  
Console.WriteLine("\n=============================\n");           
                                                                  
dv = new Device() {                                               
    name = "ABC",                                                 
    Files = new Files[] {                                         
        new Files() { Id = 1},                                    
        new Files() { Id = 2},                                    
        new Files() { Id = 3}}                                    
};                                                                
                                                                  
serializer.Serialize(Console.Out, dv);

Open in new window

0
 
Deepak SubburajanDB DeveloperCommented:
USe XMLSerializer.


XmlSerializer serializer = new XmlSerializer(typeof(Book));
using (StringReader reader = new StringReader(xmlDocumentText))
{
    Book book = (Book)(serializer.Deserialize(reader));
}

Open in new window

0
 
RuttensoftAuthor Commented:
Thanks.

So the code is:
        private void Parse()
        {
            string str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<device id=\"iPhone\">" +
            "<name>Test</name>" +
            "<year>2014</year>" +
            "<files count=\"2\">" +
            "   <file id=\"01\" name=\"File1\" username=\"bond\" />" +
            "   <file id=\"02\" name=\"File2\" username=\"james\" />" +
            "</files>" +
            "</device>";
            XmlSerializer serializer = new XmlSerializer(typeof(device));
            using (StringReader reader = new StringReader(str))
            {
                device device = (device)(serializer.Deserialize(reader));
            }

        }

        public class device
        {
            public string name { get; set; }
            public int year { get; set; }
            public List<file> files { get; set; }
            public class file
            {
                public string id { get; set; }
                public string name { get; set; }
                public string username { get; set; }
            }

        }

Open in new window


This is working for the name/year, but doesnt work for the "files"

Any suggestions?
0
 
RuttensoftAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.