developer tip

XML 직렬화-배열의 루트 요소 렌더링 비활성화

optionbox 2020. 7. 25. 10:50
반응형

XML 직렬화-배열의 루트 요소 렌더링 비활성화


컬렉션의 루트 요소 렌더링을 어떻게 비활성화 할 수 있습니까?

직렬화 속성이있는이 클래스 :

[XmlRoot(ElementName="SHOPITEM", Namespace="")]
public class ShopItem
{
    [XmlElement("PRODUCTNAME")]
    public string ProductName { get; set; }       

    [XmlArrayItem("VARIANT")]
    public List<ShopItem> Variants { get; set; }
}

이 XML을 생성합니다.

<SHOPITEM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <PRODUCTNAME>test</PRODUCTNAME>
      <Variants>
          <VARIANT>
              <PRODUCTNAME>hi 1</PRODUCTNAME>
          </VARIANT>
          <VARIANT>
              <PRODUCTNAME>hi 2</PRODUCTNAME>
          </VARIANT>           
      </Variants>        
</SHOPITEM>

나는 <Variants>여기에 요소를 원하지 않습니다 . 어떻게해야합니까?

또한 루트 요소에 xsi 및 xsd 네임 스페이스가 필요하지 않습니다 ...


컬렉션의 루트 요소 렌더링을 비활성화하려면 코드에서 속성 [XmlArrayItem][XmlElement]바꿔야합니다 .

xsixsd네임 스페이스 를 제거하려면 XmlSerializerNamespaces빈 네임 스페이스 인스턴스를 생성하고 객체를 직렬화해야 할 때 전달합니다.

이 예제를 살펴보십시오.

[XmlRoot("SHOPITEM")]
public class ShopItem
{
    [XmlElement("PRODUCTNAME")]
    public string ProductName { get; set; }

    [XmlElement("VARIANT")] // was [XmlArrayItem]
    public List<ShopItem> Variants { get; set; }
}

// ...

ShopItem item = new ShopItem()
{
    ProductName = "test",
    Variants    = new List<ShopItem>()
    {
        new ShopItem{ ProductName = "hi 1" },
        new ShopItem{ ProductName = "hi 2" }
    }
};

// This will remove the xsi/xsd namespaces from serialization
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

XmlSerializer ser = new XmlSerializer(typeof(ShopItem));
ser.Serialize(Console.Out, item, ns);  // Inform the XmlSerializerNamespaces here

나는이 출력을 얻었다 :

<?xml version="1.0" encoding="ibm850"?>
<SHOPITEM>
  <PRODUCTNAME>test</PRODUCTNAME>
  <VARIANT>
    <PRODUCTNAME>hi 1</PRODUCTNAME>
  </VARIANT>
  <VARIANT>
    <PRODUCTNAME>hi 2</PRODUCTNAME>
  </VARIANT>
</SHOPITEM>

교체 [XmlArrayItem("VARIANT")]와 함께 [XmlElement("VARIANT")].


I don't believe it is possible to remove this element using the default xml serialization (with attributes). If you could do this, then serializing your ShopItem class would result in badly formed xml (no root element) for the object, which is not allowed.

What you can do however, is manually implement IXmlSerializable. This will give you the sort of fine-grained control you a re after.

[Edit] - sorry - misread that you were trying to remove Variants, not SHOPITEM. To remove the List "outer" element, just mark it up with an [XmlElement] attribute rather than an [XmlArrayItem] attribute. This will cause the list entries to just use the specified element name, without wrapping the list in an outer element.

For removing the namespaces, this is controlled by the seriliazer itself, not the markup on the class. I've just noticed that while I've updated this answer, Rubens Farias has provided an reply that shows you how to eliminate the namespace.

참고URL : https://stackoverflow.com/questions/2006482/xml-serialization-disable-rendering-root-element-of-array

반응형