英文:
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("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}
得分: 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; }
}
</code></pre>
<p>结果如下:</p>
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;"><code><span style="display:flex;"><span><span style="color:#75715e"><?xml version="1.0" encoding="utf-8"?></span>
</span></span><span style="display:flex;"><span><span style="color:#f92672"><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">></span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672"><Cards></span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672"><Card></span>VISA<span style="color:#f92672"></Card></span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672"><Card></span>AMEX<span style="color:#f92672"></Card></span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672"><Card></span>DISCOVER<span style="color:#f92672"></Card></span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672"></Cards></span>
</span></span><span style="display:flex;"><span><span style="color:#f92672"></P_M></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(&quot;Cards&quot;)]</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e"> [XmlArrayItem(ElementName= &quot;Card&quot;)]</span>
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">public</span> List&lt;<span style="color:#66d9ef">string</span>&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>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
</span></span><span style="display:flex;"><span>&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;
</span></span><span style="display:flex;"><span> &lt;Cards&gt;
</span></span><span style="display:flex;"><span> &lt;Card&gt;VISA&lt;/Card&gt;
</span></span><span style="display:flex;"><span> &lt;Card&gt;AMEX&lt;/Card&gt;
</span></span><span style="display:flex;"><span> &lt;Card&gt;DISCOVER&lt;/Card&gt;
</span></span><span style="display:flex;"><span> &lt;/Cards&gt;
</span></span><span style="display:flex;"><span>&lt;/P_M&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>
```