xmlserializer가 널리 사용 가능한 로 실질화 할 수 있습니까?
-
08-07-2019 - |
문제
표시 할 수있는 요소가 포함 된 XML 메시지를 제조하고 싶었습니다. nil="true"
유형의 속성을 가진 클래스로 int?
. 내가 일할 수있는 유일한 방법은 내 자신을 쓰는 것입니다. NullableInt
구현하는 유형 IXmlSerializable
. 더 좋은 방법이 있습니까?
나는 전체 문제와 내가 해결 한 방식을 썼습니다. 내 블로그에서.
해결책
XMLSerializer가 NULL에 대한 버려지려면 NIL = "True"를 네임 스페이스로 접두사로 접두사해야한다고 생각합니다.
<?xml version="1.0" encoding="UTF-8"?>
<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="array">
<entity>
<id xsi:type="integer">1</id>
<name>Foo</name>
<parent-id xsi:type="integer" xsi:nil="true"/>
다른 팁
내 수정 사항은 노드를 사전 프로세스하고 "nil"속성을 수정하는 것입니다.
public static void FixNilAttributeName(this XmlNode @this)
{
XmlAttribute nilAttribute = @this.Attributes["nil"];
if (nilAttribute == null)
{
return;
}
XmlAttribute newNil = @this.OwnerDocument.CreateAttribute("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance");
newNil.Value = nilAttribute.Value;
@this.Attributes.Remove(nilAttribute);
@this.Attributes.Append(newNil);
}
나는 이것을 하위 노드에 대한 재귀 검색과 결합시켜 주어진 xmlnode (또는 xmldocument)의 경우 사막화 전에 단일 통화를 발행 할 수 있도록합니다. 원래의 메모리 구조를 변형되지 않은 상태로 유지하려면 xmlnode의 클론 ()로 작업하십시오.
예외적으로 게으른 방법. 여러 가지 이유로 연약하지만 내 XML은 빠르고 더러운 수정을 보증 할만 큼 간단합니다.
xmlStr = Regex.Replace(xmlStr, "nil=\"true\"", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"");
제휴하지 않습니다 StackOverflow