简体   繁体   中英

Deserializing an XML file using C# where an Element has Attributes and a Value

I am attempting to deserialize a file created by a different piece of software. I can't seem to figure out one part where an Element name has 2 Attributes and has Value itself.

Any help would be greatly appreciated:


        public class Channel
        {
            [XmlAttribute("channelNumber")]
            public string channelNumber;
            [XmlAttribute("status")]
            public string status;
            [XmlAttribute("type")]
            public string type;
            [XmlAttribute("ca")]
            public string ca;
            [XmlAttribute("shortName")]
            public string shortName;
            [XmlAttribute("outOfBand")]
            public string outOfBand;

            //[XmlElement]
            //[XmlAnyElement("Name")]
            //[XmlAnyElement]

            [XmlElement("Name")]
            public NameClass Name;
        }


        //[XmlRoot(ElementName = "Name")]
        public class NameClass
        {
            //[XmlElement(Order = 1)]
            //[XmlAttribute]

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

            //[XmlIgnore]
            //[XmlElement(Order = 2)]
            //[XmlAttribute("xmlns")]
            //[XmlAttribute]
            //public string xmlns;

            //[XmlElement("Name")]

            [XmlText]
            public string Value { get; set; }
        }

I left all the things I tried in...the part of the XML file is below:


      <Channel channelNumber="1" status="active" type="dt" ca="false" shortName="CH" outOfBand="true">
        <Name lang="eng" xmlns="http://www.atsc.org/XMLSchemas/pmcp/2007/3.1">CH</Name>
      </Channel>
      <ScheduleName>2020-05-06 CH Log</ScheduleName>

The part I can't read is getting the Value from Name "CH" and the Attributes of Name ("lang" and "xmlns") they always just come up null?

Change the attribute

[XmlElement("Name")]
public NameClass Name;

to

 [XmlElement("Name", Namespace = "http://www.atsc.org/XMLSchemas/pmcp/2007/3.1")]
 public NameClass Name;

Your <Name> element is defined in the selected namespace by using the attribute xmlns="" . You have to specify that you expect such an element as well.

Check the following source code:

public class Test
{
    public class Channel
    {
        [XmlElement("Name", Namespace = "http://www.atsc.org/XMLSchemas/pmcp/2007/3.1")]
        public NameClass Name;
    }    

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

    public static void Main(string[] args) {

        string xmlContent = "<Channel channelNumber=\"1\" status=\"active\" type=\"dt\" ca=\"false\" shortName=\"CH\" outOfBand=\"true\">\n"+
                            "    <Name lang=\"eng\" xmlns=\"http://www.atsc.org/XMLSchemas/pmcp/2007/3.1\">CH</Name>\n"+
                            "</Channel>";
        Console.WriteLine("XML Content:");
        Console.WriteLine(xmlContent);
        XmlSerializer serializer = new XmlSerializer(typeof(Channel));
        using (TextReader reader = new StringReader(xmlContent))
        {
            Channel result = (Channel) serializer.Deserialize(reader);
            Console.WriteLine(result);
            Console.WriteLine(result.Name);
            Console.WriteLine(result.Name.Value);
        }
    }
}

This will generate the following output:

XML Content:
<Channel channelNumber="1" status="active" type="dt" ca="false" shortName="CH" outOfBand="true">
    <Name lang="eng" xmlns="http://www.atsc.org/XMLSchemas/pmcp/2007/3.1">CH</Name>
</Channel>
Test+Channel
Test+NameClass
CH

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