英文:
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
class Market
{
[XmlArray(Order = 0)]
[XmlArrayItem(IsNullable = false)]
public List<Option> Options { get; set; }
[XmlElement(Order = 1)]
public MarketType MarketType { get; set; }
[XmlAttribute]
public int Id {get;set;}
[XmlAttribute]
public string Name {get;set;}
[XmlAttribute]
public bool IsEnabled {get;set;}
.
.
.
// like this 17 XML attribute are there
}
<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.
<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
class Market
{
[XmlArray(Order = 0)]
[XmlArrayItem(IsNullable = false)]
public List<Option> Options { get; set; }
[XmlElement(Order = 1)]
public MarketType MarketType { get; set; }
[XmlAttribute]
public int Id {get;set;}
[XmlAttribute]
public string Name {get;set;}
[XmlAttribute]
public bool IsEnabled {get;set;}
.
.
.
// like this 17 XML attribute are there
}
<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.
<Market b="" a="" IsEnabled="false" ... Name="Test" Id="10" c="" d="">
答案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进行自定义序列化
class Market : IXmlSerializable
{
[XmlAttribute]
public int Id { get; set; }
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public bool IsEnabled { get; set; }
// 类似这样的 17 个 XML 属性
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("Id", Id.ToString());
writer.WriteAttributeString("Name", Name);
writer.WriteAttributeString("IsEnabled", IsEnabled.ToString());
}
public void ReadXml(XmlReader reader)
{
XElement element = (XElement)XElement.ReadFrom(reader);
foreach (XElement node in element.Elements())
{
}
foreach (XAttribute attribute in element.Attributes())
{
switch (attribute.Name.LocalName)
{
case "Id":
Id = (int)attribute;
break;
case "Name":
Name = (string)attribute;
break;
case "IsEnabled":
IsEnabled = (Boolean)attribute;
break;
}
}
}
public XmlSchema GetSchema()
{
return (null);
}
}
英文:
Use a custom serialization using IXmlSerializable
<!-- begin snippet: js hide: false console: true babel: false -->
class Market : IXmlSerializable
{
[XmlAttribute]
public int Id {get;set;}
[XmlAttribute]
public string Name {get;set;}
[XmlAttribute]
public bool IsEnabled {get;set;}
// like this 17 XML attribute are there
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("Id", Id.ToString());
writer.WriteAttributeString("Name", Name);
writer.WriteAttributeString("IsEnabled", IsEnabled.ToString());
}
public void ReadXml(XmlReader reader)
{
XElement element = (XElement)XElement.ReadFrom(reader);
foreach(XElement node in element.Elements())
{
}
foreach (XAttribute attribute in element.Attributes())
{
switch (attribute.Name.LocalName)
{
case "Id" :
Id = (int)attribute;
break;
case "Name":
Name = (string)attribute;
break;
case "IsEnabled":
IsEnabled = (Boolean)attribute;
break;
}
}
}
public XmlSchema GetSchema()
{
return (null);
}
}
<!-- end snippet -->
答案3
得分: 0
Here is the translated code:
找到了正确的解决方案,我从 XmlArray 和 XmlElement 中移除了 order,然后它开始正常工作。
现在我的类看起来像这样:
class Market
{
[XmlArray]
[XmlArrayItem(IsNullable = false)]
public List<Option> Options { get; set; }
[XmlElement]
public MarketType MarketType { get; set; }
[XmlAttribute]
public int Id { get; set; }
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public bool IsEnabled { get; set; }
.
.
.
// 这样一共有 17 个 XML 属性
}
英文:
Got a proper solution, I removed order from XmlArray and XmlElement and it started working fine,
Now my class looks like this
class Market
{
[XmlArray]
[XmlArrayItem(IsNullable = false)]
public List<Option> Options { get; set; }
[XmlElement]
public MarketType MarketType { get; set; }
[XmlAttribute]
public int Id {get;set;}
[XmlAttribute]
public string Name {get;set;}
[XmlAttribute]
public bool IsEnabled {get;set;}
.
.
.
// like this 17 XML attribute are there
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论