Link to home
Start Free TrialLog in
Avatar of FrancineTaylor
FrancineTaylor

asked on

Still trying to duplicate an existing XML format

Here is what the XML format for the Spec class needs to look like:

<Spec>
    <Param Name="DimX">
        <Value TypeCode="6">14</Value>
    </Param>
    <Param Name="DimY">
        <Value TypeCode="7">27</Value>
    </Param>
</Spec>

Here is the class structure that I have built:

[Serializable()]
public class Spec
{
    public List<cParam> Param { get; set; }
    public Spec()
    {
        Param = new List<cParam>();
    }
}
[Serializable()]
public class cParam
{
    [XmlAttribute]
    public string Name { get; set; }
    public int Value { get; set; }
    public cParam() { }
}

Here's what the output looks like when I serialize it with XmlSerializer:

<Spec>
    <Param>
        <Param Name="DimX">
            <Value >14</Value>
        </Param>
        <Param Name="DimY">
            <Value >27</Value>
        </Param>
    <Param>
</Spec>

1. How do I get rid of the extra Param layer?
2. How do I add the TypeCode property?

This is just a small, one-time project that I am doing at the end of my contract, so please don't "answer" my question with a link to a commercial product.  I've got things mostly working, I just need a couple of tweaks to the format.
Avatar of deepu chandran
deepu chandran
Flag of Germany image

Hi,

Change your class like this,
 [XmlRoot("Spec")]   
    public class Spec
    {
        public List<cParam> Param { get; set; }
        public Spec()
        {
            Param = new List<cParam>();
        }
    }
    [Serializable()]
    public class cParam
    {
        [XmlAttribute]
        public string Name { get; set; }
        public int Value { get; set; }
        public cParam() { }
    }

Open in new window


Use serializing like this,

var xmlNsEmpty = new XmlSerializerNamespaces();
            xmlNsEmpty.Add(string.Empty, string.Empty);

            using (StringWriter sw = new StringWriter())
            {
                var xs = new XmlSerializer(objSpec.GetType());
                xs.Serialize(sw, objSpec,xmlNsEmpty);
                string xmlEncodedList = sw.ToString();
            }

Open in new window


Thanks
Deepu
Avatar of FrancineTaylor
FrancineTaylor

ASKER

Thanks for the code, Deepu, but it didn't answer either of my questions.  The code that is being produced looks like this:

<Spec>
    <Param>
        <Param Name="DimX">
            <Value >14</Value>
        </Param>
        <Param Name="DimY">
            <Value >27</Value>
        </Param>
    <Param>
</Spec>

It need to look like this:

<Spec>
        <Param Name="DimX">
            <Value  TypeCode="8">14</Value>
        </Param>
        <Param Name="DimY">
            <Value  TypeCode="9">27</Value>
        </Param>
</Spec>

Sorry if I didn't make that clear enough...
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!  This is exactly what I needed.
Your welcome, glad I could help! :)