简体   繁体   中英

How do I deserialize XML attribute?

I'm fairly new to C#, so I hope you guys can guide me here. I have the following XML which should be converted to an object with code, description and lang, but I'm really struggling to get the 'lang' attribute value of the XML.

Currently it only return values for 'code' and 'description', while 'lang' return null.

<?xml version="1.0" encoding="UTF-8"?>
<statisticgroup>
  <code>2049</code>
  <description lang="en-GB">2049</description>
</statisticgroup>
  [Serializable]
  public class XmlStatisticsModel
  {
    [XmlElement ( "code" )]
    public string Code { get; set; }

    [XmlElement ( "description" )]
    public string Description { get; set; }

    [XmlAttribute ( "lang" )]
    public string Lang { get; set; }
  }

You can't read language attribute this way because it is not an attribute of the <statisticgroup> node (which represents your class) but an attribute of the <Description> node. Change Description to a class and specify the attribute there, as follows:

        [Serializable]
        public class Description
        {
            [XmlText]
            public string Value { get; set; }

            [XmlAttribute("lang")]
            public string Lang { get; set; }
        }

        [Serializable]
        [XmlRoot("statisticgroup")]
        public class XmlStatisticsModel
        {
            [XmlElement("code")]
            public string Code { get; set; }

            [XmlElement("description")]
            public Description Description { get; set; }
        }

    static void Main(string[] args)
    {
        string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<statisticgroup>
  <code>2049</code>
  <description lang=""en-GB"">2049</description>
</statisticgroup>
";
        StringReader sr = new StringReader(xml);
        XmlStatisticsModel statisticsModel = (XmlStatisticsModel)new XmlSerializer(typeof(XmlStatisticsModel)).Deserialize(sr);
        Console.WriteLine("Description: {0} (lang: {1})", statisticsModel.Description.Value, statisticsModel.Description.Lang);

    }

define an XSD for your XML schema and let Microsofts xsd.exe generate the class definition for you.

To get a good starting point for your XSD, you can also generate the XSD from your XML. But there is some work to do on the autogenerated XSD.

try this code:

class Program
{
    static void Main(string[] args)
    {
        string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
        <statisticgroup>
            <code>2049</code>
            <description lang=""en-GB"">2049</description>
        </statisticgroup>
        ";
        var sr = new StringReader(xml);
        var deserialized = new XmlSerializer(typeof(Statisticgroup)).Deserialize(sr);
    }
}

[XmlRoot(ElementName = "description")]
public class Description
{
    [XmlAttribute(AttributeName = "lang")]
    public string Lang { get; set; }
    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName = "statisticgroup")]
public class Statisticgroup
{
    [XmlElement(ElementName = "code")]
    public string Code { get; set; }
    [XmlElement(ElementName = "description")]
    public Description Description { get; set; }
}

results in:

在此处输入图片说明

In this case you are defining the Description as a string but in this case you have to define it as its own element type:

[XmlRoot(ElementName="description")]
public class Description 
{
    [XmlAttribute ( "lang" )]
    public string Lang { get; set; }
    [XmlText]
    public string Text { get; set; }
}

Then inside the main object you define it as:

    [XmlElement(ElementName="description")]
    public Description Description { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM