Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

XML parsing - how?

Posted on 2014-08-21
4
Medium Priority
?
128 Views
Last Modified: 2014-08-21
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
0
Comment
Question by:Ruttensoft
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:Deepak Subburajan
ID: 40276042
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
 

Author Comment

by:Ruttensoft
ID: 40276058
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
 
LVL 1

Accepted Solution

by:
Deepak Subburajan earned 2000 total points
ID: 40276119
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
 

Author Closing Comment

by:Ruttensoft
ID: 40276188
Thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question