2012-01-22 74 views
0

我有一个xml文件。我需要序列化为c#类。c#xml序列化问题

<Configs> 
    <Services> 
    <Path> 
     <Url>test</Url> 
     <ID>1234</ID> 
    </Path> 
     <Path> 
     <Url>java</Url> 
     <ID>1234</ID> 
    </Path> 
    </Services> 
    <Certification> 
     <name>Mark</name> 
     <name>Peter</name> 
    </Certification> 
    </Configs> 
    </Configuration> 

我需要序列化到一个类

我写了下面的这个类。

public class Configuration 
     { 
      public Certification Configs { get; set; } 
      public Path[] Services { get; set; } 
     } 

     public class Certification 
     { 
      [XmlArray("Certification")] 
      [XmlArrayItem("name")] 
      public string[] name { get; set; } 

      } 

     public class Path 
     { 
      public string Url { get; set; } 
      public string ID { get; set; } 
     } 

我得到认证类的价值和它的价值。 我创建了一个对象配置和循环

foreach (string Name in obj.Configs.name) 
{ 
} 

这是工作

但我没有得到服务的价值。 这不是工作越来越空的obj.services

Foreach (Path serviceUrl in obj.Services) 
    { 
    } 

obj.Services是空

如何获得URL和ID节点的路径中的价值?

感谢

回答

2

Services阵列是在错误的类。它应该在根类的Configs属性中:

public class Configuration 
{ 
    public Certification Configs { get; set; } 
} 

public class Certification 
{ 
    [XmlArray("Certification")] 
    [XmlArrayItem("name")] 
    public string[] name { get; set; } 

    public Path[] Services { get; set; } 
}