내 클래스에 따라 역 직렬화하려는 XML이 있습니다. 올바르게 역 직렬화되지만 일부 값은 null이됩니다. 오류가 발생하지 않으며 오류가 어디에 있는지 잘 모르겠습니다.
클래스를 변경하고 메모리 모델을 직렬화 한 다음 출력을 확인하려고 시도했지만 원하는대로 작동하지 않았습니다. 제공된 XML을 따라야합니다.
내 모델 :
[XmlRoot(ElementName = "model", Namespace = "http://www.archimatetool.com/archimate")]
public class Model
{
[XmlElement(ElementName = "folder")]
public List<Folder> Folders { get; set; }
[XmlElement(ElementName = "purpose")]
public string Purpose { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "archimate", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Archimate { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
}
내 XML
<?xml version="1.0" encoding="UTF-8"?>
<archimate:model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:archimate="http://www.archimatetool.com/archimate" name="Archisurance" id="11f5304f" version="3.1.1">
<folder name="Business" id="8c90fdfa" type="business">
<folder name="Actors" id="fa63373b">
<element xsi:type="archimate:BusinessInterface" id="1544" name="mail"/>
</folder>
</folder>
<purpose>An example of a fictional Insurance company.</purpose>
</archimate:model>
이것은 직렬화 해제 후 얻은 결과입니다. (평판 때문에) 사진을 게시 할 수 없어서 링크 만 게시하고 있습니다.
목적 필드에 "가상 보험 회사의 예"가 표시 될 것으로 예상하지만 null입니다.
다음 데이터 모델을 사용하여 XML을 역 직렬화 할 수 있습니다.
[XmlRoot(ElementName = "model", Namespace = "http://www.archimatetool.com/archimate")]
[XmlType(Namespace = "http://www.archimatetool.com/archimate")]
public class Model
{
[XmlElement(ElementName = "folder", Form = XmlSchemaForm.Unqualified)]
public List<Folder> Folders { get; set; }
[XmlElement(ElementName = "purpose", Form = XmlSchemaForm.Unqualified)]
public string Purpose { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
}
[XmlType(Namespace = "http://www.archimatetool.com/archimate")]
public class Folder
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlElement(ElementName = "folder", Form = XmlSchemaForm.Unqualified)]
public List<Folder> Folders { get; set; }
[XmlElement(ElementName = "element", Form = XmlSchemaForm.Unqualified)]
public List<Element> Element { get; set; }
}
[XmlType(Namespace = "http://www.archimatetool.com/archimate")]
[XmlInclude(typeof(BusinessInterface))]
public abstract class Element
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
[XmlType(TypeName = "BusinessInterface", Namespace = "http://www.archimatetool.com/archimate")]
public class BusinessInterface : Element
{
}
메모:
루트 요소 <archimate:model>
는 archimate:
네임 스페이스에 있지만 archimate:
네임 스페이스가 기본 네임 스페이스가 아니기 때문에 자식 요소는 네임 스페이스에 없습니다. 따라서 XmlSerializer
이러한 자식 요소가 부모와 다른 네임 스페이스 에 있음을 표시해야 합니다. 설정이 XmlElementAttribute.Form = XmlSchemaForm.Unqualified
이를 수행합니다.
(달리 지정하지 않는 한 모든 XML 속성은 규정되지 않은 것으로 간주되므로 속성이 기본 네임 스페이스에 있음을 지정할 필요가 없습니다.)
xsi:type="archimate:BusinessInterface"
속성 의 존재는 <element>
속성이 다형성 유형 계층의 일부 임을 나타냅니다 . xsi:type
속성은이다 표준 W3C 속성 의 유형 주장을 명시 적으로 요소 수 있습니다. XmlSerializer
이 속성을 지원하며 실제로 속성을 xsi:type
통해 선언 된 및에 해당하는 하위 유형이 있어야 [XmlInclude]
합니다.
자세한 내용은 방법 : 파생 클래스의 직렬화 제어 를 참조하십시오 .
여기에서는 기본 클래스 Element
와 파생 클래스 에 포함 할 속성을 임의로 선택했습니다 BusinessInterface
. 보다 완전한 XML 샘플이 주어지면이 선택을 조정해야 할 수도 있습니다.
여기에 샘플 바이올린 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다