XML attribute not getting namespace prefix

前端 未结 4 1207
说谎
说谎 2020-12-12 00:01

I need to generate the following XML during serialization: (fragment)


  

        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 00:12

    I'll give KKD credit for the answer, but I discovered another scenario that still causes issues. Apparently if the object to be serialized is a child of another object, if the parent object's namespace is the same as the child's, the serializer assumes you don't need to explicitly declare the namespace for the child.

     public class IncidentEvent : IXmlSerializable
     {
        public string EventDate { get; set; }
        public string EventTime { get; set; }
        public string EventTypeText { get; set; }
    
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }
    
        public void ReadXml(System.Xml.XmlReader reader)
        {
            return null;
        }
    
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("ex", "EventTypeText", "http://foo", EventTypeText);
        }
     }
    

    By implementing IXmlSerializable, I can manually write out the elements and attributes in exactly the way I need. Since this is a one-way export, I didn't need to implement anything but the WriteXml method.

    I'm still not sure if this is the best way, but it works for the moment.

提交回复
热议问题