【问题标题】:Writing XML attribute in-line rather than with <value>...</value> tags内联而不是使用 <value>...</value> 标记编写 XML 属性
【发布时间】:2018-08-22 14:59:23
【问题描述】:

下面是我正在编写的一些 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;
        }
    }
}

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

【问题讨论】:

  • 已经在这里解释了stackoverflow.com/questions/1096797/…
  • @Y.S 这个答案似乎很好地解释了每种风格的优缺点,但这不是我的问题。我特别问如何删除 ... 标记并内联写入值。

标签: c# xml xsd


【解决方案1】:

通过在 sharedTypeSiteSpecificCustomAttribute 类上实现 IXmlSerializable 解决

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

【讨论】:

    猜你喜欢
    • 2012-12-18
    • 2021-08-30
    • 2017-10-14
    • 1970-01-01
    • 2014-03-08
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多