英文:
C# List<> property to XML
问题
这是您提供的代码片段的翻译:
获得了这段代码...
public class CardLogos
{
[XmlElement("Card")]
public string Card { get; set; }
}
public class P_M
{
[XmlElement("Cards")]
public List<CardLogos> Cards { get; set; }
}
尝试构建Cards属性:
pm = new P_M();
...其他属性设置发生在这里
pm.Cards = new List<CardLogos>();
pm.Cards.Add(new CardLogos() {Card="VISA"});
pm.Cards.Add(new CardLogos() {Card="AMEX"});
pm.Cards.Add(new CardLogos() {Card="DISCOVER"});
但序列化的XML输出如下:
<P_M>
<Other Properties>
<Cards>
<Card>VISA</Card>
</Cards>
<Cards>
<Card>AMEX</Card>
</Cards>
<Cards>
<Card>DISCOVER</Card>
</Cards>
</P_M>
如何使其输出如下:
<P_M>
<Other Properties>
<Cards>
<Card>VISA</Card>
<Card>AMEX</Card>
<Card>DISCOVER</Card>
</Cards>
</P_M>
英文:
Got this code...
public class CardLogos
{
[XmlElement("Card")]
public string Card { get; set; }
}
public class P_M
{
[XmlElement("Cards")]
public List<CardLogos> Cards { get; set; }
}
Trying to build the Cards property:
pm = new P_M();
...Other property setting happens here
pm.Cards = new List<CardLogos>();
pm.Cards.Add(new CardLogos() {Card="VISA"});
pm.Cards.Add(new CardLogos() {Card="AMEX"});
pm.Cards.Add(new CardLogos() {Card="DISCOVER"});
But the serialized XML comes out like this:
<P_M>
<Other Properties>
<Cards>
<Card>VISA</Card>
</Cards>
<Cards>
<Card>AMEX</Card>
</Cards>
<Cards>
<Card>DISCOVER</Card>
</Cards>
</P_M>
How can I make it come out like this:
<P_M>
<Other Properties>
<Cards>
<Card>VISA</Card>
<Card>AMEX</Card>
<Card>DISCOVER</Card>
</Cards>
</P_M>
答案1
得分: 1
试试这个:
```csharp
public class P_M
{
[XmlArray("Cards")]
[XmlArrayItem(ElementName= "Card")]
public List<CardLogos> Cards { get; set; }
}
public class CardLogos
{
[XmlText]
public string Card { get; set; }
}
它保留了所有原始类型,而不需要将其简化为 List<string>
。CardLogos
中可能有的其他属性可以使用 XmlIgnore
属性跳过。
<details>
<summary>英文:</summary>
Try this:
public class P_M
{
[XmlArray("Cards")]
[XmlArrayItem(ElementName= "Card")]
public List<CardLogos> Cards { get; set; }
}
public class CardLogos
{
[XmlText]
public string Card { get; set; }
}
It preserves all of the original types, without needing to reduce down to a `List<string>`. Any other properties you may have in `CardLogos` can be skipped over with `XmlIngore` attributes.
</details>
# 答案2
**得分**: 0
你可以使用 [`XmlArray`][1] 和 [`XmlArrayItem`][2] 属性,并将 `Cards` 改为字符串列表:
```csharp
public class P_M
{
[XmlArray("Cards")]
[XmlArrayItem(ElementName = "Card")]
public List<string> Cards { get; set; }
}
结果如下:
<?xml version="1.0" encoding="utf-8"?>
<P_M xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Cards>
<Card>VISA</Card>
<Card>AMEX</Card>
<Card>DISCOVER</Card>
</Cards>
</P_M>
查看示例。
英文:
You can use the XmlArray
and XmlArrayItem
attributes and change Cards
to a list of strings:
public class P_M
{
[XmlArray("Cards")]
[XmlArrayItem(ElementName= "Card")]
public List<string> Cards { get; set; }
}
This results in:
<?xml version="1.0" encoding="utf-8"?>
<P_M xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Cards>
<Card>VISA</Card>
<Card>AMEX</Card>
<Card>DISCOVER</Card>
</Cards>
</P_M>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论