سؤال

My xml has attributes.how can i declare those variables for deserialization. xml is as follows.

<LabelRequest   Test="NO" LabelType="DEFAULT" LabelSubtype="NONE" LabelSize="4x6" ImageFormat="PNG">
 <MailpieceShape>FLAT</MailpieceShape>
 <Services Open="On" />
 <Options mail="off" />
</LabelRequest>

I tried like this.

  public class Requester
{ 
  public string LabelRequest { get; set; }
    [XmlAttribute]
    public string Test { get; set; }
    [XmlAttribute]
    public string LabelType { get; set; }
    [XmlAttribute]
    public string LabelSubtype { get; set; }
    [XmlAttribute]
    public string LabelSize { get; set; }
    [XmlAttribute]
    public string ImageFormat { get; set; }
    public string MailpieceShape { get; set; }

}

But its showing like there is an error in Xml Document.How should i declare the attributes?

هل كانت مفيدة؟

المحلول

This is working 100%. I've just changed mail property to Mail so it correct with naming convetion for properties.

class Program
{
    static void Main()
    {
        Requester objectToDeserialize;

        using (Stream stream = File.Open("file.xml", FileMode.Open))
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(Requester));
            objectToDeserialize = (Requester)deserializer.Deserialize(stream);
        }

        Console.WriteLine(objectToDeserialize.Test);
        Console.WriteLine(objectToDeserialize.LabelType);
        Console.WriteLine(objectToDeserialize.LabelSubtype);
        Console.WriteLine(objectToDeserialize.LabelSize);
        Console.WriteLine(objectToDeserialize.ImageFormat);
        Console.WriteLine(objectToDeserialize.MailpieceShape);
        Console.WriteLine(objectToDeserialize.Services.Open);
        Console.WriteLine(objectToDeserialize.Options.Mail);

        Console.ReadLine();
    }
}

[XmlRoot(ElementName = "LabelRequest")]
public class Requester
{
    [XmlAttribute]
    public string Test { get; set; }
    [XmlAttribute]
    public string LabelType { get; set; }
    [XmlAttribute]
    public string LabelSubtype { get; set; }
    [XmlAttribute]
    public string LabelSize { get; set; }
    [XmlAttribute]
    public string ImageFormat { get; set; }
    public string MailpieceShape { get; set; }
    public Services Services { get; set; }
    public Options Options { get; set; }
}

public class Services
{
    [XmlAttribute]
    public string Open { get; set; }
}

public class Options
{
    [XmlAttribute]
    public string Mail { get; set; }
}

XML for this example:

<?xml version="1.0" encoding="utf-8" ?>
<LabelRequest Test="NO" LabelType="DEFAULT" LabelSubtype="NONE" LabelSize="4x6" ImageFormat="PNG">
  <MailpieceShape>FLAT</MailpieceShape>
  <Services Open="On" />
  <Options Mail="off" />
</LabelRequest>

نصائح أخرى

Your class should be like this:

    public class LabelRequest
{
    [XmlAttribute]
    public string Test { get; set; }

    [XmlAttribute]
    public string LabelType { get; set; }

    [XmlAttribute]
    public string LabelSubtype { get; set; }

    [XmlAttribute]
    public string LabelSize { get; set; }

    [XmlAttribute]
    public string ImageFormat { get; set; }

    public string MailpieceShape { get; set; }

    public Services Services { get; set; }

    public Options Options { get; set; }
}

public class Services
{
    public string Open { get; set; }
}

public class Options
{
    public string mail { get; set; }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top