C# 使用C语言反序列化XML数组#
如果我有如下XML:C# 使用C语言反序列化XML数组#,c#,arrays,xml,serialization,C#,Arrays,Xml,Serialization,如果我有如下XML: <?xml version="1.0" encoding="UTF-8" ?> <Config> <Interface> <Theme>Dark</Theme> <Mode>Advanced</Mode> </Interface> <Export> <Destination>\\ser
<?xml version="1.0" encoding="UTF-8" ?>
<Config>
<Interface>
<Theme>Dark</Theme>
<Mode>Advanced</Mode>
</Interface>
<Export>
<Destination>\\server1.example.com</Destination>
<Destination>\\server2.example.com</Destination>
<Destination>\\server3.example.com</Destination>
</Export>
</Config>
我应该如何在“导出”部分反序列化“目标”元素的数组,以便我可以循环通过数组对象来打印值
i、 e
您需要在列表的声明中添加带有
Destination
标识符的xmlement
标记,以便填充它
[Serializable, XmlRoot("Config")]
public class Config
{
public Interface Interface { get; set; }
public Export Export { get; set; }
}
public struct Export
{
[XmlElement("Destination")]
public List<string> Destinations { get; set; }
}
您还可以通过添加XmlText
标记来创建自定义Destination
类,而不是使用字符串列表
public struct Export
{
[XmlElement("Destination")]
public List<Destination> Destinations { get; set; }
}
public struct Destination
{
[XmlText()]
public string Value { get; set; }
}
foreach (Destination destination in config.Export.Destinations)
{
Console.WriteLine(destination.Value);
}
公共结构导出
{
[XmlElement(“目的地”)]
公共列表目标{get;set;}
}
公共结构目标
{
[XmlText()]
公共字符串值{get;set;}
}
foreach(config.Export.Destinations中的目标)
{
Console.WriteLine(destination.Value);
}
您需要将带有目的地
标识符的xmlement
标记添加到列表的声明中,以便填充它
[Serializable, XmlRoot("Config")]
public class Config
{
public Interface Interface { get; set; }
public Export Export { get; set; }
}
public struct Export
{
[XmlElement("Destination")]
public List<string> Destinations { get; set; }
}
您还可以通过添加XmlText
标记来创建自定义Destination
类,而不是使用字符串列表
public struct Export
{
[XmlElement("Destination")]
public List<Destination> Destinations { get; set; }
}
public struct Destination
{
[XmlText()]
public string Value { get; set; }
}
foreach (Destination destination in config.Export.Destinations)
{
Console.WriteLine(destination.Value);
}
公共结构导出
{
[XmlElement(“目的地”)]
公共列表目标{get;set;}
}
公共结构目标
{
[XmlText()]
公共字符串值{get;set;}
}
foreach(config.Export.Destinations中的目标)
{
Console.WriteLine(destination.Value);
}
您可以这样使用:cs公共类导出{public List Destination{get;set;}}}公共类配置{public Interface Interface Interface{get;set;}公共导出导出{get;set;}}
我确实尝试过,但当我尝试执行Console.WriteLine时,它只会导致“索引超出范围”(config.Export.Destination[0]);
您可以这样使用:cs公共类导出{公共列表目标{get;set;}}}公共类配置{公共接口接口{get;set;}公共导出{get;set;}
我确实尝试过,但当我尝试执行Console.WriteLine(config.Export.Destination[0]);
@Quantum\u Kernel时,它只会导致“索引超出范围”;@Quantum\u Kernel我做了一个可能感兴趣的小编辑you@Quantum_Kernel我做了一个小编辑,你可能会感兴趣
public struct Export
{
[XmlElement("Destination")]
public List<Destination> Destinations { get; set; }
}
public struct Destination
{
[XmlText()]
public string Value { get; set; }
}
foreach (Destination destination in config.Export.Destinations)
{
Console.WriteLine(destination.Value);
}