user386258
user386258

Reputation: 1953

c#Xml serialisation issue

I have an xml file.i need to serialize to c# class.

<Configs>
     <Services>
    <Path>
      <Url>test</Url>
      <ID>1234</ID>
    </Path>
      <Path>
      <Url>java</Url>
      <ID>1234</ID>
    </Path>
    </Services>
    <Certification>
      <name>Mark</name>
      <name>Peter</name>
    </Certification>
    </Configs>
  </Configuration>

I need to serialize this into a class

I have written the following class.

 public class Configuration
        {
            public Certification Configs { get; set; }
            public Path[] Services { get; set; }
        }

        public class  Certification
        {
            [XmlArray("Certification")]
            [XmlArrayItem("name")]
            public string[] name { get; set; }

          }

        public class Path
        {
            public string Url { get; set; }
            public string ID { get; set; }
        }

I am getting the value of Certification class and its value. I created an object for Configuration and looped

foreach (string Name in obj.Configs.name)
{
}

this is working.

But i am not getting the value of Services. This is not working getting null on obj.services

   Foreach (Path serviceUrl in obj.Services)
    {
    }

obj.Services is null

How to get the value of url and ID node in Path?

Thanks

Upvotes: 0

Views: 73

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Your Services array is in the wrong class. It should be inside the Configs property of the root class:

public class Configuration
{
    public Certification Configs { get; set; }
}

public class Certification
{
    [XmlArray("Certification")]
    [XmlArrayItem("name")]
    public string[] name { get; set; }

    public Path[] Services { get; set; }
}

Upvotes: 2

Related Questions