简体   繁体   中英

How to rename `xmlns` as TYPE using C#

I'm trying to create a XML file format by using c#. Ii almost done. But, the output not yet come as I expect.

my current output is

   <?xml version="1.0"?> 
<ArrayOfMESSAGE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">   
        <MESSAGE>
        <HEADER xmlns="M_DAT">
        </HEADER>
        <DATA_SET>

        </DATA_SET>   
        </MESSAGE> 
   </ArrayOfMESSAGE>
<MSG />

program.cs

using (FileStream fs = new FileStream("D:\\read.xml", FileMode.Create))
            {
                using (XmlWriter writer = XmlWriter.Create(fs, new XmlWriterSettings() { OmitXmlDeclaration = true, Indent = true }))
                {lstQTM);
                    writer.WriteStartElement("MSG");
                    new XmlSerializer(typeof(List<QTMList>)).Serialize(fs, lstQTMlist);
                    writer.WriteEndElement();
                }
            }

QTMList.cs

[XmlType("MESSAGE")]
    //[XmlElement("MSG")]
    public class QTMList
    {
        [XmlElement(Namespace = "M_DAT" ,ElementName ="HEADER")]
        public List<QTMMain> QTMMain { get; set; }
        [XmlElement(ElementName = "DATA_SET")]
        public List<QTMdataset> QTMdataset { get; set; }
    }

My desire output is

<MSG>
<MESSAGE>
<HEADER Type="M_DAT"> 
</HEADER>
<DATA_SET SampleSize="5">
</DATA_SET>
</MESSAGE>
</MSG>

how can I change

xmlns

as TYPE

Please give me some hint

I have change my QTMMain class as

 public class QTMMain
    {
        [XmlAttribute("TYPE")]
        public string TYPE { get; set; }

        [XmlText]
        public string Value { get; set; }
        public string DEVICE_TYPE { get; set; }
        public string DEVICE_ID { get; set; }
        //[XmlType(TypeName = "DEVICE_TYPE")]
        public string SWITCH_ID { get; set; }
        //[XmlType(TypeName = "DATE_TIME")]
        public string DATE_TIME { get; set; }
        public List<QTMStats> QTMStats { get; set; }
        public List<QTM> QTM { get; set; }
}

I have added

[XmlAttribute("TYPE")]
public string TYPE { get; set; }
[XmlText]
public string Value { get; set; }

lines into above class.then it printed as I expect.

Thank you @Chris Walsh sir to give me a hint.

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