2014-02-17 5 views
1

다음과 같은 XML이 있습니다. 이것을 C# 객체로 변환하고 싶습니다. 나는 modyfying을 시도했지만 작동시키지 못했습니다.개체에 XML 비 직렬화가 작동하지 않습니다.

<SLVGeoZone-array> 
    <SLVGeoZone> 
    <id>19</id> 
    <type>geozone</type> 
    <name>60_OLC_SC</name> 
    <namesPath>GeoZones/60_OLC_SC</namesPath> 
    <idsPath>1/19</idsPath> 
    <childrenCount>0</childrenCount> 
    </SLVGeoZone> 
    </SLVGeoZone-array> 

나는이 서면 샘플 C# 코드와는 작동하지 않습니다

[Serializable] 
public class SLVGeoZone 
{ 
    [XmlElement("id")] 
    public string id { get; set; } 

    [XmlElement("type")] 
    public string type { get; set; } 

    [XmlElement("name")] 
    public string name { get; set; } 

    [XmlElement("namespath")] 
    public string namespath { get; set; } 

    [XmlElement("idspath")] 
    public string idspath { get; set; } 

    [XmlElement("childrencount")] 
    public string childrencount { get; set; } 
} 

[Serializable] 
[XmlRoot("SLVGeoZone-array")] 
public class SLVGeoZone-array 
{ 
    [XmlArray("SLVGeoZone-array")] 
    [XmlArrayItem("SLVGeoZone", typeof(SLVGeoZone))] 
    public SLVGeoZone[] Car { get; set; } 
} 

그리고 형태

:

XmlSerializer serializer = new XmlSerializer(typeof(CarCollection)); 
StreamReader reader = new StreamReader(path); 
cars = (CarCollection)serializer.Deserialize(reader); 
reader.Close(); 

누군가가 내가 뭘 잘못 제안 할 수 있습니까?

+0

당신은 "작동하지 않습니다"무엇을 의미합니까? 예외가 발생합니까? 빈 객체를 반환합니까? 엘더 차원에서 말로 표현할 수없는 공포가 뒤틀린다고? – Aron

답변

1
  1. SLVGeoZone-array

    [Serializable()] 
    [XmlRoot("SLVGeoZone-array")] 
    public class SLVGeoZones 
    { 
        [XmlElement("SLVGeoZone")] 
        public SLVGeoZone[] Cars { get; set; } 
    } 
    
  2. XmlElement 속성 값은 XML 파일에서 요소 이름과 정확하게 일치해야 C#에서 유효한 클래스 이름이 아닙니다. 너는 그렇지 않아.

    [Serializable()] 
    public class SLVGeoZone 
    { 
        [XmlElement("id")] 
        public string id { get; set; } 
    
        [XmlElement("type")] 
        public string type { get; set; } 
    
        [XmlElement("name")] 
        public string name { get; set; } 
    
        [XmlElement("namesPath")] 
        public string namespath { get; set; } 
    
        [XmlElement("idsPath")] 
        public string idspath { get; set; } 
    
        [XmlElement("childrenCount")] 
        public string childrencount { get; set; } 
    } 
    
  3. 무엇 CarCollection이며 왜 당신 대신 당신이 여기에 표시 한 클래스의 CarCollection로 XML을 역 직렬화하려고?

    XmlSerializer serializer = new XmlSerializer(typeof(SLVGeoZones)); 
    StreamReader reader = new StreamReader("Input.txt"); 
    var items = (SLVGeoZones)serializer.Deserialize(reader); 
    reader.Close(); 
    
+0

양해 해 주셔서 감사합니다. 효과가있었습니다. – user1687824

관련 문제