删除名称空间(xsd,xs1和)在XML序列化c#

本文关键字:XML quot 序列化 空间 xs1 xsd 删除 版本 | 更新日期: 2023-09-27 18:04:55

我有两个类Room和CHILD_RATE类:当我序列化数据时,我得到下面提到的xml(示例1):但是我希望删除名称空间(xsd,xs1和)。我想在样本2中输出:format

[XmlRoot]

 public class Room
    {
        [XmlElement]
        public int Occupancy { get; set; }
        [XmlElement]
        public int Quantity { get; set; }
        [XmlArray(ElementName="CHILDREN")]
        public List<CHILD_RATE> Childs { get; set; }
    }

    public class CHILD_RATE
    {
        [XmlAttribute]
        public int CHILD_AGE { get; set; }
        [XmlAttribute]
        public int CHILD_QUANTITY { get; set; }
    }
Sample 1:

 <?xml version="1.0"?>
<ArrayOfRoom xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Room>
    <Occupancy>7</Occupancy>
    <Quantity>3</Quantity>
    <CHILDREN>
      <CHILD_RATE CHILD_AGE="1" CHILD_QUANTITY="2" />
      <CHILD_RATE CHILD_AGE="4" CHILD_QUANTITY="1" />
    </CHILDREN>
  </Room>
  <Room>
    <Occupancy>8</Occupancy>
    <Quantity>3</Quantity>
    <CHILDREN>
      <CHILD_RATE CHILD_AGE="3" CHILD_QUANTITY="3" />
      <CHILD_RATE CHILD_AGE="7" CHILD_QUANTITY="2" />
    </CHILDREN>
  </Room>
  <Room>
    <Occupancy>2</Occupancy>
    <Quantity>1</Quantity>
    <CHILDREN />
  </Room>
  <Room>
    <Occupancy>5</Occupancy>
    <Quantity>1</Quantity>
    <CHILDREN />
  </Room>
</ArrayOfRoom>

我想输出如下:

样本2:

 <Room>
    <Occupancy>7</Occupancy>
    <Quantity>3</Quantity>
    <CHILDREN>
      <CHILD_RATE CHILD_AGE="1" CHILD_QUANTITY="2" />
      <CHILD_RATE CHILD_AGE="4" CHILD_QUANTITY="1" />
    </CHILDREN>
  </Room>
  <Room>
    <Occupancy>8</Occupancy>
    <Quantity>3</Quantity>
    <CHILDREN>
      <CHILD_RATE CHILD_AGE="3" CHILD_QUANTITY="3" />
      <CHILD_RATE CHILD_AGE="7" CHILD_QUANTITY="2" />
    </CHILDREN>
  </Room>
  <Room>
    <Occupancy>2</Occupancy>
    <Quantity>1</Quantity>
    <CHILDREN />
  </Room>
  <Room>
    <Occupancy>5</Occupancy>
    <Quantity>1</Quantity>
    <CHILDREN />
  </Room>
对于序列化,我使用:
public string CreaateXML(List<Room> room)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Room>));
        MemoryStream stream = new MemoryStream();
        serializer.Serialize(stream, room);
        return Encoding.UTF8.GetString(stream.ToArray());
    }

。e,我不想要

 <?xml version="1.0"?>
 <ArrayOfRoom xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance">

删除名称空间(xsd,xs1和<?)XML版本="1.0"?>)在XML序列化c#

可以创建一个空的命名空间。根据您的代码修改

public string CreaateXML(List<Room> room)
{
        var xmlnamespace = new Original.XmlSerializerNamespaces();
        xmlnamespace.Add(string.Empty, string.Empty);
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.OmitXmlDeclaration = omitDeclaration;
        settings.Encoding = encoding;
        XmlWriter writer = XmlWriter.Create(ms, settings);

        XmlSerializer serializer = new XmlSerializer(typeof(List<Room>));
        MemoryStream stream = new MemoryStream();
        serializer.Serialize(writer, room, xmlnamespace);
        return Encoding.UTF8.GetString(stream.ToArray());
}
 XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            MemoryStream ms = new MemoryStream();
            XmlWriter writer = XmlWriter.Create(ms, settings);
            XmlSerializerNamespaces names = new XmlSerializerNamespaces();
            names.Add("", "");
            XmlSerializer cs = new XmlSerializer(typeof(List<Room>));
            cs.Serialize(writer, room, names);
            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            StreamReader sr = new StreamReader(ms);
            var xml = sr.ReadToEnd();
            return xml;