XmlAttribute顺序或序列在序列化.NET类时出现问题。

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

XmlAttribute order or sequence going wrong while serializing .net class

问题

I have an existing class where we have many public properties. Most of them are decorated with [XmlAttribute]. Issue is when I have 16 member then xml show order of attribute correctly

  1. class Market
  2. {
  3. [XmlArray(Order = 0)]
  4. [XmlArrayItem(IsNullable = false)]
  5. public List<Option> Options { get; set; }
  6. [XmlElement(Order = 1)]
  7. public MarketType MarketType { get; set; }
  8. [XmlAttribute]
  9. public int Id {get;set;}
  10. [XmlAttribute]
  11. public string Name {get;set;}
  12. [XmlAttribute]
  13. public bool IsEnabled {get;set;}
  14. .
  15. .
  16. .
  17. // like this 17 XML attribute are there
  18. }
  19. <Market Id="10" Name="Test" IsEnabled="false" ... a="" b="" c="" d="">

When I have 17 attribute then it brings 3rd Last and 4th Last attribute in the start and 1st properties goes to 3rd last and 4th last position, i.e. order got disturbed. I tried shuffling element, it only swaps 1st 2 attribute with 3rd and 4th last attribute.

  1. <Market b="" a="" IsEnabled="false" ... Name="Test" Id="10" c="" d="">
英文:

I have a existing class where we have many public properties. Most of them are decorated with [XmlAttribute]. Issue is when I have 16 member then xml show order of attribute correctly

  1. class Market
  2. {
  3. [XmlArray(Order = 0)]
  4. [XmlArrayItem(IsNullable = false)]
  5. public List&lt;Option&gt; Options { get; set; }
  6. [XmlElement(Order = 1)]
  7. public MarketType MarketType { get; set; }
  8. [XmlAttribute]
  9. public int Id {get;set;}
  10. [XmlAttribute]
  11. public string Name {get;set;}
  12. [XmlAttribute]
  13. public bool IsEnabled {get;set;}
  14. .
  15. .
  16. .
  17. // like this 17 XML attribute are there
  18. }
  19. &lt;Market Id=&quot;10&quot; Name=&quot;Test&quot; IsEnabled=&quot;false&quot; ... a=&quot;&quot; b=&quot;&quot; c=&quot;&quot; d=&quot;&quot;&gt;

When I have 17 attribute then it brings 3rd Last and 4th Last attribute in the start and 1st properties goes to 3rd last and 4th last position, i.e. order got disturbed. I tried shuffling element, it only swaps 1st 2 attribute with 3rd and 4th last attribute.

  1. &lt;Market b=&quot;&quot; a=&quot;&quot; IsEnabled=&quot;false&quot; ... Name=&quot;Test&quot; Id=&quot;10&quot; c=&quot;&quot; d=&quot;&quot;&gt;

答案1

得分: 3

XML中不能依赖属性的顺序:https://stackoverflow.com/a/57757267/20771004

如果顺序很重要,您可能希望将它们改为元素。

英文:

You can’t rely on the order of attributes in XML: https://stackoverflow.com/a/57757267/20771004

If order is important, you might want to make them elements instead.

答案2

得分: 1

使用IXmlSerializable进行自定义序列化

  1. class Market : IXmlSerializable
  2. {
  3. [XmlAttribute]
  4. public int Id { get; set; }
  5. [XmlAttribute]
  6. public string Name { get; set; }
  7. [XmlAttribute]
  8. public bool IsEnabled { get; set; }
  9. // 类似这样的 17 个 XML 属性
  10. public void WriteXml(XmlWriter writer)
  11. {
  12. writer.WriteAttributeString("Id", Id.ToString());
  13. writer.WriteAttributeString("Name", Name);
  14. writer.WriteAttributeString("IsEnabled", IsEnabled.ToString());
  15. }
  16. public void ReadXml(XmlReader reader)
  17. {
  18. XElement element = (XElement)XElement.ReadFrom(reader);
  19. foreach (XElement node in element.Elements())
  20. {
  21. }
  22. foreach (XAttribute attribute in element.Attributes())
  23. {
  24. switch (attribute.Name.LocalName)
  25. {
  26. case "Id":
  27. Id = (int)attribute;
  28. break;
  29. case "Name":
  30. Name = (string)attribute;
  31. break;
  32. case "IsEnabled":
  33. IsEnabled = (Boolean)attribute;
  34. break;
  35. }
  36. }
  37. }
  38. public XmlSchema GetSchema()
  39. {
  40. return (null);
  41. }
  42. }
英文:

Use a custom serialization using IXmlSerializable

<!-- begin snippet: js hide: false console: true babel: false -->

  1. class Market : IXmlSerializable
  2. {
  3. [XmlAttribute]
  4. public int Id {get;set;}
  5. [XmlAttribute]
  6. public string Name {get;set;}
  7. [XmlAttribute]
  8. public bool IsEnabled {get;set;}
  9. // like this 17 XML attribute are there
  10. public void WriteXml(XmlWriter writer)
  11. {
  12. writer.WriteAttributeString(&quot;Id&quot;, Id.ToString());
  13. writer.WriteAttributeString(&quot;Name&quot;, Name);
  14. writer.WriteAttributeString(&quot;IsEnabled&quot;, IsEnabled.ToString());
  15. }
  16. public void ReadXml(XmlReader reader)
  17. {
  18. XElement element = (XElement)XElement.ReadFrom(reader);
  19. foreach(XElement node in element.Elements())
  20. {
  21. }
  22. foreach (XAttribute attribute in element.Attributes())
  23. {
  24. switch (attribute.Name.LocalName)
  25. {
  26. case &quot;Id&quot; :
  27. Id = (int)attribute;
  28. break;
  29. case &quot;Name&quot;:
  30. Name = (string)attribute;
  31. break;
  32. case &quot;IsEnabled&quot;:
  33. IsEnabled = (Boolean)attribute;
  34. break;
  35. }
  36. }
  37. }
  38. public XmlSchema GetSchema()
  39. {
  40. return (null);
  41. }
  42. }

<!-- end snippet -->

答案3

得分: 0

Here is the translated code:

  1. 找到了正确的解决方案,我从 XmlArray XmlElement 中移除了 order,然后它开始正常工作。
  2. 现在我的类看起来像这样:
  3. class Market
  4. {
  5. [XmlArray]
  6. [XmlArrayItem(IsNullable = false)]
  7. public List<Option> Options { get; set; }
  8. [XmlElement]
  9. public MarketType MarketType { get; set; }
  10. [XmlAttribute]
  11. public int Id { get; set; }
  12. [XmlAttribute]
  13. public string Name { get; set; }
  14. [XmlAttribute]
  15. public bool IsEnabled { get; set; }
  16. .
  17. .
  18. .
  19. // 这样一共有 17 个 XML 属性
  20. }
英文:

Got a proper solution, I removed order from XmlArray and XmlElement and it started working fine,
Now my class looks like this

  1. class Market
  2. {
  3. [XmlArray]
  4. [XmlArrayItem(IsNullable = false)]
  5. public List&lt;Option&gt; Options { get; set; }
  6. [XmlElement]
  7. public MarketType MarketType { get; set; }
  8. [XmlAttribute]
  9. public int Id {get;set;}
  10. [XmlAttribute]
  11. public string Name {get;set;}
  12. [XmlAttribute]
  13. public bool IsEnabled {get;set;}
  14. .
  15. .
  16. .
  17. // like this 17 XML attribute are there
  18. }

huangapple
  • 本文由 发表于 2023年6月8日 18:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430903.html
匿名

发表评论

匿名网友

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

确定