0

I want to deseralize the following XML in C#

<Configuration>
    <Parameters>
        <Parameter AttrName1 = "AttrValue1"/>
        <Parameter AttrName2 = "AttrValue2"/>
        <Parameter AttrName3 = "AttrValue3"/>
    </Parameters>
</Configuration>

I am having trouble because Attributes names and values are all differents for the same value.

Thank you so much.

3
  • Paste XML as Classes, just make that valid xml first Commented Mar 7, 2018 at 18:02
  • That Xml is my input. I would need to read, update then probably save. Commented Mar 7, 2018 at 18:06
  • Out of interest why do you need three different parameter names. They could all be called AttrName for example. Just curious. 😀 Commented Mar 7, 2018 at 18:21

1 Answer 1

2

You can use these classes to serialize/deserialize your xml. Note this is a feature in VS: Paste XML as Classes

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Configuration
{

    private ConfigurationParameter[] parametersField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("Parameter", IsNullable = false)]
    public ConfigurationParameter[] Parameters
    {
        get
        {
            return this.parametersField;
        }
        set
        {
            this.parametersField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigurationParameter
{

    private string attrName1Field;

    private string attrName2Field;

    private string attrName3Field;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string AttrName1
    {
        get
        {
            return this.attrName1Field;
        }
        set
        {
            this.attrName1Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string AttrName2
    {
        get
        {
            return this.attrName2Field;
        }
        set
        {
            this.attrName2Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string AttrName3
    {
        get
        {
            return this.attrName3Field;
        }
        set
        {
            this.attrName3Field = value;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Never knew about that feature. I wrote mine manually. I am going to see if I can use this to compare the results in my C# project with my own code..👀

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.