2013-10-23 3 views
0

나는 다음과 같은 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 태그를 추가하려고했지만 작동하지 않았습니다. 너희들은 좋은 조언 :

+0

클래스 'c'는 어디입니까? – sloth

+0

sry 내 나쁜, 그냥 내 게시물을 편집! – Leviathan

+0

아래 내 대답을 확인하십시오. 또한 PascalCase 및 필드, camelCase를 사용하는 로컬 변수를 사용하여 클래스, 속성 및 메서드의 이름을 지정하는 C# 코드 규칙을 따르십시오. –

답변

2

코드에 두 가지 문제점이 있습니다.

1) de/serialize 할 클래스 멤버의 액세스 한정자를 public과 같이 선언하십시오. 기본 직렬화 구현은 공용 멤버에서만 작동합니다.

[XmlRoot(ElementName = "a")] 
public class a 
{ 
    [XmlElement("b")] 
    public List<b> bs = new List<b>(); 
} 

2) 당신은 당신의 경우 클래스 e에서, 하나의 객체에서 여러 [XmlText]을 선언 할 수 없습니다. 대신 [XmlElement]으로 변경하십시오.

public class e 
{ 
    [XmlElement] 
    public String f { get; set; } 
    [XmlElement] 
    public String g { get; set; } 
    [XmlElement] 
    public String h { get; set; } 
    [XmlElement] 
    public String i { get; set; } 
} 

그러면 작동합니다.

+0

꽤 잘 일하고 있습니다, 정말 고마워요 !!! – Leviathan

0
XElement xmlFile= new XElement("a", 
           new XElement("b", 
            new XElement("c", 
             new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")), 
             new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")), 
            new XElement("c", 
             new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")), 
             new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")), 
            new XElement("Department", new XAttribute("Name","Automobile")) 
           ), 
           new XElement("b", 
            new XElement("c", 
             new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")), 
             new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")), 
            new XElement("c", 
             new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")), 
             new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")) 
           ) 

) 나에게 큰 호의를 할 것; xmlFile.Save (@ "D : \ file.xml");

문제 해결에 도움이 될 경우이 방법을 사용해보십시오. 그리고 당신은 같은 방식으로 비 직렬화 할 수 있습니다.

편집 :

시도도 코드에서 마지막 줄을 추가합니다. 나는 코드 섹션의 일부를 추가하려고 시도했지만 어떻게 든 일어나지 않는다.

+0

빠른 답장을 보내신 Thx, 그걸 확인해 보겠습니다. – Leviathan

관련 문제