클래스를 XML에 serialize하려는 경우. 작동하지만 "origen"문자열 속성은 항상 비어있을 때 문자열로 직렬화됩니다.XmlSerialization이 비어있는 경우 문자열 특성을 무시합니다.
FirmaElement firma= new FirmaElement();
firma.Value="HITHERE";
firma.origen=String.Empty;
예상 된 결과
string x= Serialize(FirmaElement);
x="<Firma>HITHERE</Firma>";
FirmaElement firma= new FIrmaElement();
firma.Value="HITHERE";
firma.origen="OK";
:이 클래스는, 예를 들어 FirmaElement입니다
null의 경우 나는 XML 내부를 포함 시리얼을 피하려고 예상 결과
string x= Serialize(FirmaElement);
x="<Firma origen='ok'>HITHERE</Firma>";
코드
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://gen/ces/headers/CesHeader")]
[System.Xml.Serialization.XmlRoot("Firma")]
public class FirmaElement
{
public FirmaElement() { }
string _origen = String.Empty;
string _value = String.Empty;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string origen
{
get { return _origen; }
set { _origen = value; }
}
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get { return _value; }
set { _value = value; }
}
public override string ToString()
{
return this.Value;
}
//IS THIS CORRECT? SHould i override something??
public string Serialize()
{
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
/* Setting XmlIgnore to false overrides the XmlIgnoreAttribute
applied to the Comment field. Thus it will be serialized.*/
attrs.XmlIgnore = String.IsNullOrEmpty(origen);
xOver.Add(typeof(string), "origen", attrs);
I DONT KNOW WHAT TO PUT HERE, IT'S CORRECT??
//XmlSerializer xSer = new XmlSerializer(typeof(XmlTypeAttribute), xOver);
}
}
가치없는 속성을 처음 표시 할 때의 해가되는 점은 무엇입니까? XML은 제공된 값의 유무와 관계없이 데이터 형식을 처리하도록 설계되었습니다. –
[Xml serialization - null 값 숨기기] 가능한 복제본 (http://stackoverflow.com/questions/5818513/xml-serialization-hide-null-values) –