0

Below is a sample of some XML that I am writing. This is valid XML, but in an attempt to cut down the file size I'd like to remove the tags and simply write the value in-line with the attribute.

What I currently have

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

What I'd prefer to have

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

List holding custom attributes and extending list add method

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

Add method extension

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 code

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 code

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;
        }
    }
}

Any help is appreciated. This is new to me and I'm just trying to learn the best practices and approaches.

2
  • already explained here stackoverflow.com/questions/1096797/… Commented Aug 22, 2018 at 15:13
  • @Y.S That answer seems great for explaining the advantages and disadvantages to each style, but that isn't my question. I am asking specifically how to remove the <value>...</value> tags and write the values in-line. Commented Aug 22, 2018 at 15:19

1 Answer 1

0

Solved by implementing IXmlSerializable on class sharedTypeSiteSpecificCustomAttribute

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteAttributeString("attribute-id", attributeid);
        writer.WriteValue(value);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.