C# List<> 属性到 XML

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

C# List<> property to XML

问题

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

  1. 获得了这段代码...
  2. public class CardLogos
  3. {
  4. [XmlElement("Card")]
  5. public string Card { get; set; }
  6. }
  7. public class P_M
  8. {
  9. [XmlElement("Cards")]
  10. public List<CardLogos> Cards { get; set; }
  11. }
  12. 尝试构建Cards属性:
  13. pm = new P_M();
  14. ...其他属性设置发生在这里
  15. pm.Cards = new List<CardLogos>();
  16. pm.Cards.Add(new CardLogos() {Card="VISA"});
  17. pm.Cards.Add(new CardLogos() {Card="AMEX"});
  18. pm.Cards.Add(new CardLogos() {Card="DISCOVER"});
  19. 但序列化的XML输出如下:
  20. <P_M>
  21. <Other Properties>
  22. <Cards>
  23. <Card>VISA</Card>
  24. </Cards>
  25. <Cards>
  26. <Card>AMEX</Card>
  27. </Cards>
  28. <Cards>
  29. <Card>DISCOVER</Card>
  30. </Cards>
  31. </P_M>
  32. 如何使其输出如下:
  33. <P_M>
  34. <Other Properties>
  35. <Cards>
  36. <Card>VISA</Card>
  37. <Card>AMEX</Card>
  38. <Card>DISCOVER</Card>
  39. </Cards>
  40. </P_M>
英文:

Got this code...

  1. public class CardLogos
  2. {
  3. [XmlElement(&quot;Card&quot;)]
  4. public string Card { get; set; }
  5. }
  6. public class P_M
  7. {
  8. [XmlElement(&quot;Cards&quot;)]
  9. public List&lt;CardLogos&gt; Cards { get; set; }
  10. }

Trying to build the Cards property:

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

But the serialized XML comes out like this:

  1. &lt;P_M&gt;
  2. &lt;Other Properties&gt;
  3. &lt;Cards&gt;
  4. &lt;Card&gt;VISA&lt;/Card&gt;
  5. &lt;/Cards&gt;
  6. &lt;Cards&gt;
  7. &lt;Card&gt;AMEX&lt;/Card&gt;
  8. &lt;/Cards&gt;
  9. &lt;Cards&gt;
  10. &lt;Card&gt;DISCOVER&lt;/Card&gt;
  11. &lt;/Cards&gt;
  12. &lt;/P_M&gt;

How can I make it come out like this:

  1. &lt;P_M&gt;
  2. &lt;Other Properties&gt;
  3. &lt;Cards&gt;
  4. &lt;Card&gt;VISA&lt;/Card&gt;
  5. &lt;Card&gt;AMEX&lt;/Card&gt;
  6. &lt;Card&gt;DISCOVER&lt;/Card&gt;
  7. &lt;/Cards&gt;
  8. &lt;/P_M&gt;

答案1

得分: 1

  1. 试试这个:
  2. ```csharp
  3. public class P_M
  4. {
  5. [XmlArray("Cards")]
  6. [XmlArrayItem(ElementName= "Card")]
  7. public List<CardLogos> Cards { get; set; }
  8. }
  9. public class CardLogos
  10. {
  11. [XmlText]
  12. public string Card { get; set; }
  13. }

它保留了所有原始类型,而不需要将其简化为 List<string>CardLogos 中可能有的其他属性可以使用 XmlIgnore 属性跳过。

  1. <details>
  2. <summary>英文:</summary>
  3. 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; }
}

  1. 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.
  2. </details>
  3. # 答案2
  4. **得分**: 0
  5. 你可以使用 [`XmlArray`][1] [`XmlArrayItem`][2] 属性,并将 `Cards` 改为字符串列表:
  6. ```csharp
  7. public class P_M
  8. {
  9. [XmlArray("Cards")]
  10. [XmlArrayItem(ElementName = "Card")]
  11. public List<string> Cards { get; set; }
  12. }

结果如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <P_M xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3. <Cards>
  4. <Card>VISA</Card>
  5. <Card>AMEX</Card>
  6. <Card>DISCOVER</Card>
  7. </Cards>
  8. </P_M>

查看示例

英文:

You can use the XmlArray and XmlArrayItem attributes and change Cards to a list of strings:

  1. public class P_M
  2. {
  3. [XmlArray(&quot;Cards&quot;)]
  4. [XmlArrayItem(ElementName= &quot;Card&quot;)]
  5. public List&lt;string&gt; Cards { get; set; }
  6. }

This results in:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &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;
  3. &lt;Cards&gt;
  4. &lt;Card&gt;VISA&lt;/Card&gt;
  5. &lt;Card&gt;AMEX&lt;/Card&gt;
  6. &lt;Card&gt;DISCOVER&lt;/Card&gt;
  7. &lt;/Cards&gt;
  8. &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:

确定