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.
C#XML

Avatar of undefined
Last Comment
mccarl

8/22/2022 - Mon
deepu chandran

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
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
mccarl

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
FrancineTaylor

ASKER
Thank you!  This is exactly what I needed.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
mccarl

Your welcome, glad I could help! :)