51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

C# List 属性到 XML

英文:

C# List<> property to XML

问题 {#heading}

这是您提供的代码片段的翻译:

获得了这段代码...

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}

得分: 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&lt;string&gt; Cards { get; set; }
}
</code></pre>
 <p>结果如下:</p>
 <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;"><code><span style="display:flex;"><span><span style="color:#75715e">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;P_M</span> <span style="color:#a6e22e">xmlns:xsi=</span><span style="color:#e6db74">"http://www.w3.org/2001/XMLSchema-instance"</span> <span style="color:#a6e22e">xmlns:xsd=</span><span style="color:#e6db74">"http://www.w3.org/2001/XMLSchema"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;Cards&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;Card&gt;</span>VISA<span style="color:#f92672">&lt;/Card&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;Card&gt;</span>AMEX<span style="color:#f92672">&lt;/Card&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;Card&gt;</span>DISCOVER<span style="color:#f92672">&lt;/Card&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/Cards&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/P_M&gt;</span>
</span></span></code></pre>
 <p><a href="https://dotnetfiddle.net/lQmwO7" rel="external nofollow" target="_blank">查看示例</a>。</p>
 <details>
  <summary>英文:</summary>
  <p>You can use the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlarrayattribute?view=net-7.0"><code>XmlArray</code></a> and <a href="https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlarrayitemattribute?view=net-7.0"><code>XmlArrayItem</code></a> attributes and change <code>Cards</code> to a list of strings:</p>
  <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;"><code><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">P_M</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">    [XmlArray(&amp;quot;Cards&amp;quot;)]</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">	[XmlArrayItem(ElementName= &amp;quot;Card&amp;quot;)]</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> List&amp;lt;<span style="color:#66d9ef">string</span>&amp;gt; Cards { <span style="color:#66d9ef">get</span>; <span style="color:#66d9ef">set</span>; }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre>
  <p>This results in:</p>
  <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;"><code><span style="display:flex;"><span>&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;
</span></span><span style="display:flex;"><span>&amp;lt;P_M xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot; xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&amp;gt;
</span></span><span style="display:flex;"><span>  &amp;lt;Cards&amp;gt;
</span></span><span style="display:flex;"><span>    &amp;lt;Card&amp;gt;VISA&amp;lt;/Card&amp;gt;
</span></span><span style="display:flex;"><span>    &amp;lt;Card&amp;gt;AMEX&amp;lt;/Card&amp;gt;
</span></span><span style="display:flex;"><span>    &amp;lt;Card&amp;gt;DISCOVER&amp;lt;/Card&amp;gt;
</span></span><span style="display:flex;"><span>  &amp;lt;/Cards&amp;gt;
</span></span><span style="display:flex;"><span>&amp;lt;/P_M&amp;gt;
</span></span></code></pre>
  <p><a href="https://dotnetfiddle.net/lQmwO7" rel="external nofollow" target="_blank">See this example</a>.</p>
 </details>
 <p></p>
</div>

```
赞(0)
未经允许不得转载:工具盒子 » C# List 属性到 XML