gpt4 book ai didi

c# - C# 中的 XML 序列化数组

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:04 24 4
gpt4 key购买 nike

我无法解决这个问题,我有一个看起来像这样的 xml 表

<root>
<list id="1" title="One">
<word>TEST1</word>
<word>TEST2</word>
<word>TEST3</word>
<word>TEST4</word>
<word>TEST5</word>
<word>TEST6</word>
</list>
<list id="2" title="Two">
<word>TEST1</word>
<word>TEST2</word>
<word>TEST3</word>
<word>TEST4</word>
<word>TEST5</word>
<word>TEST6</word>
</list>
</root>

我正在尝试将其序列化为

public class Items
{
[XmlAttribute("id")]
public string ID { get; set; }

[XmlAttribute("title")]
public string Title { get; set; }

//I don't know what to do for this
[Xml... something]
public list<string> Words { get; set; }
}

//I don't this this is right either
[XmlRoot("root")]
public class Lists
{
[XmlArray("list")]
[XmlArrayItem("word")]
public List<Items> Get { get; set; }
}

//Deserialize XML to Lists Class
using (Stream s = File.OpenRead("myfile.xml"))
{
Lists myLists = (Lists) new XmlSerializer(typeof (Lists)).Deserialize(s);
}

我是 XML 和 XML 序列化的新手,非常感谢任何帮助

最佳答案

如果您将类声明为,它应该可以工作

public class Items
{
[XmlAttribute("id")]
public string ID { get; set; }

[XmlAttribute("title")]
public string Title { get; set; }

[XmlElement("word")]
public List<string> Words { get; set; }
}

[XmlRoot("root")]
public class Lists
{
[XmlElement("list")]
public List<Items> Get { get; set; }
}

关于c# - C# 中的 XML 序列化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10743067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com