나는 다음과 같은 XML 파일이 :XML 역 직렬화하는 방법을
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>
<b attr0="">
<c>
<d attr1="" attr2="">
<e>
<f/>
<g/>
<h/>
<i/>
</e>
</d>
<!-- ...more d's -->
</c>
</b>
<b>
<c>
<d attr1="" attr2="">
<e>
<f/>
<g/>
<h/>
<i/>
</e>
</d>
<!-- ...more d's -->
</c>
</b>
<!-- ...more b's -->
</a>
나는, 나는 다음과 같은 클래스를 사용하고 C#을 객체로를 직렬화하는 :
클래스 A :
[XmlRoot(ElementName = "a")]
public class a
{
[XmlElement("b")]
List<b> bs = new List<b>();
}
을
클래스 b :
public class b
{
[XmlAttribute("attr0")]
String attr0{ get; set; }
[XmlElement("c")]
c c1 = new c();
}
Cla SS의 C :
public class c
{
[XmlElement("d")]
List<d> ds = new List<d>();
}
등급 (class) D :
public class d
{
[XmlAttribute(AttributeName = "attr1")]
String attr1{ get; set; }
[XmlAttribute(AttributeName = "attr2")]
String attr2{ get; set; }
[XmlElement("e")]
List<e> es = new List<e>();
}
및 클래스 E :
public class e
{
[XmlText]
String f { get; set; }
[XmlText]
String g { get; set; }
[XmlText]
String h { get; set; }
[XmlText]
String i { get; set; }
}
그리고 다음 코드로
내가 그것을 역 직렬화 할 :public a deserialize()
{
XmlSerializer deserializer = new XmlSerializer(typeof(a));
System.IO.TextReader reader = new System.IO.StreamReader(@"C:\file.xml");
object obj = deserializer.Deserialize(reader);
a XmlData = (a)obj;
reader.Close();
return a;
}
음 지금은 아무 것도 작동하지 않습니다. XMLArray 태그를 추가하려고했지만 작동하지 않았습니다. 너희들은 좋은 조언 :
클래스 'c'는 어디입니까? – sloth
sry 내 나쁜, 그냥 내 게시물을 편집! – Leviathan
아래 내 대답을 확인하십시오. 또한 PascalCase 및 필드, camelCase를 사용하는 로컬 변수를 사용하여 클래스, 속성 및 메서드의 이름을 지정하는 C# 코드 규칙을 따르십시오. –