一个字段可以有 XmlAttribute 和 XmlElement 属性吗?

Can a field have XmlAttribute and XmlElement attributes?

我需要通过以下任一方式 read/write 一个 xml 元素。

<element param="..." set="...">
</element>

<element>
 <param>...</param>
 <set>...</set>
</element>

<element param="...">
 <set>...</set>
</element>

<element set="...">
 <param>...</param>
</element>

是否可以 class 使用此表格来完成这项工作?

[XmlType("element")]
public class Element
{
  [XmlAttribute, XmlElement]
  public string param { get; set; }
  [XmlAttribute, XmlElement]
  public string set { get; set; }
}

您是否正在寻找一个 XML class 可以像这个一样具有同名的元素和属性?

    [XmlRootAttribute(ElementName = "element")]
    public class ElementRoot
    {
        [XmlAttribute("param")]
        public string paramAtribute;

        [XmlElement("param")]
        public string paramElement;

        [XmlAttribute("set")]
        public string setAtribute;

        [XmlElement("set")]
        public string setElement;
    }

XML:

<?xml version="1.0" encoding="UTF-8"?>
<element xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" param="..." set="...">
   <param>...</param>
   <set>...</set>
</element>