C# List<> 属性到 XML

huangapple go评论139阅读模式
英文:

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(&quot;Card&quot;)]
    public string Card { get; set; }
}

public class P_M
{
    [XmlElement(&quot;Cards&quot;)]
    public List&lt;CardLogos&gt; Cards { get; set; }
}

Trying to build the Cards property:

pm = new P_M();
...Other property setting happens here
pm.Cards = new List&lt;CardLogos&gt;();
pm.Cards.Add(new CardLogos() {Card=&quot;VISA&quot;});
pm.Cards.Add(new CardLogos() {Card=&quot;AMEX&quot;});
pm.Cards.Add(new CardLogos() {Card=&quot;DISCOVER&quot;});

But the serialized XML comes out like this:

&lt;P_M&gt;
    &lt;Other Properties&gt;
    &lt;Cards&gt;
        &lt;Card&gt;VISA&lt;/Card&gt;
    &lt;/Cards&gt;
    &lt;Cards&gt;
        &lt;Card&gt;AMEX&lt;/Card&gt;
    &lt;/Cards&gt;
    &lt;Cards&gt;
        &lt;Card&gt;DISCOVER&lt;/Card&gt;
    &lt;/Cards&gt;
&lt;/P_M&gt;

How can I make it come out like this:

&lt;P_M&gt;
    &lt;Other Properties&gt;
    &lt;Cards&gt;
        &lt;Card&gt;VISA&lt;/Card&gt;
        &lt;Card&gt;AMEX&lt;/Card&gt;
        &lt;Card&gt;DISCOVER&lt;/Card&gt;
    &lt;/Cards&gt;
&lt;/P_M&gt;

答案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&lt;string&gt;`.  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(&quot;Cards&quot;)]
	[XmlArrayItem(ElementName= &quot;Card&quot;)]
    public List&lt;string&gt; Cards { get; set; }
}

This results in:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;P_M xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
  &lt;Cards&gt;
    &lt;Card&gt;VISA&lt;/Card&gt;
    &lt;Card&gt;AMEX&lt;/Card&gt;
    &lt;Card&gt;DISCOVER&lt;/Card&gt;
  &lt;/Cards&gt;
&lt;/P_M&gt;

See this example.

huangapple
  • 本文由 发表于 2023年8月11日 02:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定