内联而不是使用<value> ... </value>标记来编写XML属性

时间:2018-08-22 14:59:23

标签: c# xml xsd

下面是我正在编写的一些XML的示例。这是有效的XML,但是为了减小文件大小,我想删除标签,并直接在属性中插入值。

我目前拥有的

<custom-attributes>
  <custom-attribute attribute-id="isDropShip">
    <value>false</value>
  </custom-attribute>
  <custom-attribute attribute-id="restrictPayPal">
    <value>false</value>
  </custom-attribute>
</custom-attributes>

我希望拥有的东西

<custom-attributes>
  <custom-attribute attribute-id="isDropShip">false</custom-attribute>
  <custom-attribute attribute-id="restrictPayPal">false</custom-attribute>
</custom-attributes>

包含自定义属性和扩展列表添加方法的列表

 List<sharedTypeSiteSpecificCustomAttribute> custom = new List<sharedTypeSiteSpecificCustomAttribute>();
 custom.AddAttribute("isDropShip", dropship);
 custom.AddAttribute("restrictPayPal", subClass);

添加方法扩展名

public static class ListExtensions
{
    public static void AddAttribute(this List<sharedTypeSiteSpecificCustomAttribute> list, string id, string value)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return;
        }
        list.Add(new sharedTypeSiteSpecificCustomAttribute { attributeid = id, value = new[] { value } });
    }
}

XSD sharedTypeSiteSpecificCustomAttribute代码

public partial class sharedTypeSiteSpecificCustomAttribute : sharedTypeCustomAttribute
{

    private string siteidField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute("site-id")]
    public string siteid
    {
        get
        {
            return this.siteidField;
        }
        set
        {
            this.siteidField = value;
        }
    }
}

sharedTypeCustomAttribute代码

public partial class sharedTypeCustomAttribute
{

    private string[] valueField;

    private string[] textField;

    private string attributeidField;

    private string langField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("value")]
    public string[] value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string[] Text
    {
        get
        {
            return this.textField;
        }
        set
        {
            this.textField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute("attribute-id")]
    public string attributeid
    {
        get
        {
            return this.attributeidField;
        }
        set
        {
            this.attributeidField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/XML/1998/namespace")]
    public string lang
    {
        get
        {
            return this.langField;
        }
        set
        {
            this.langField = value;
        }
    }
}

感谢您的帮助。这对我来说是新的,我只是想学习最佳实践和方法。

1 个答案:

答案 0 :(得分:0)

通过在类IXmlSerializable上实现sharedTypeSiteSpecificCustomAttribute来解决

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteAttributeString("attribute-id", attributeid);
        writer.WriteValue(value);
    }