내 문제는 매우 이상하게 보였으므로 그 문제는 다른 것으로 생각되지 않았으므로 매우 간단하고 어리석은 실수 인 것 같습니다. 찾아내는 것.C# XmlSerializer는 xs : attribute를 xs : string이 아닌 다른 형식으로 무시합니다.
xsd.exe를 사용하여 클래스 구조를 생성하는 XSD가 있습니다. 내 객체를 값으로 채 웁니다. 그러나 XML에 직렬화 할 때 유형이 string
이 아닌 모든 클래스 속성은 무시됩니다.
var myGraph = new graph();
myGraph.myString = "hallo";
myGraph.myInt = 80;
var serializer = new XmlSerializer(typeof(graph));
TextWriter writeFileStream = new StreamWriter(Path.Combine(outFolder, outFile));
serializer.Serialize(writeFileStream, myGraph);
writeFileStream.Close();
내가 예상 :
<graph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
myString="hallo"
myInt="80"
/>
실제 출력은 다음과 같습니다
<graph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
myString="hallo"
/>
이 속성 myInt
이 무시되었습니다. 문자열로 정의하면 표시되지만 다른 형식으로는 표시되지 않습니다. 내가 required
이라고 선언하고 null
으로두면 myInt="0"
으로 직렬화됩니다.
무엇이 누락 되었습니까?
일부 세부 정보 :
XSD :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="graph">
<xs:complexType>
<xs:attribute name="myString" type="xs:string" />
<xs:attribute name="myInt" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:schema>
생성 된 클래스 : 귀하의 측면 내 친구에가는 다른
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=false)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class graph {
private string myStringField;
private int myIntField;
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public string myString {
get { return this.myStringField; }
set { this.myStringField = value; }
}
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public int myInt {
get { return this.myIntField; }
set { this.myIntField = value; }
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool myIntSpecified {
get { return this.myIntFieldSpecified; }
set { this.myIntFieldSpecified = value; }
}
에서, myIntSpecified 기능은 myIntFieldSpecified 변수를 참조하지만, 내가 수업 시간에 어디서든 정의 된이 변수가 표시되지 않습니다 ? – pmartin