0

I have the following Xml :

<Head>
<Name>someName</Name>
<Config>
  <Section name='One'/>  
  <Section name='Two'/>
</Config>
</Head>

and I would like it to map the following object structure:

public class Head{
   public String Name {get;set;}

   public Config Config {get;set;}       

}

public class Config
{       
    public Section[] Sections { get; set; }
}

public class Section
{
    [XmlAttribute(AttributeName="name")]
    public String Name { get; set; }

}

How can I do this using Attributes only (if possible?) but without adding a top level < Sections > above < Section > elements, and also keeping the class structure. I have tried with XmlArrayItem, but i can't get the section elements.

1 Answer 1

1

Before asking the questions, I've tried the XmlArrayAttribute and the XmlArraItemAttribute with all possible combinations of namedProperty, and the attribute to be used was simply XmlElement. So I managed it like this:

just added the [XmlElement(ElementName="Section")] on top of the Sections property in the Config class. updated here below :

  public class Head{
     public String Name {get;set;}

     public Config Config {get;set;}       

  }

  public class Config
  {       
      [XmlElement(ElementName="Section")]
      public Section[] Sections { get; set; }
  }

  public class Section
  {
      [XmlAttribute(AttributeName="name")]
      public String Name { get; set; }

  }
Sign up to request clarification or add additional context in comments.

Comments

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.