0

I want to create the following xml:

 <StartLot>
      <fileCreationDate level="7">201301132210</fileCreationDate>
      <fmtVersion level="7">3.0</fmtVersion>
 </StartLot>

Below is the Serialization code:

[Serializable]
class StartLot
{
    public fileCreationDate{get; set;}

    [XmlAttribute("level")] 
    public string level = "7";

    public fmtVersion{get; set;}

    [XmlAttribute("level")] 
    public string level = "7"; ??
}

Since I already declared attribute level, how do I add the last attribute?

2
  • 1
    Yu may have to create another type, take a look at this answer stackoverflow.com/a/4154801/525138 Commented Mar 15, 2013 at 20:24
  • The [Serializable] attribute does not affect XML serialization, but it shouldn't break anything leaving it in. Commented Mar 15, 2013 at 20:32

2 Answers 2

2

You can use a wrapped class to store both values, as in the example below:

public class StackOverflow_15441384
{
    const string XML = @"<StartLot>
                               <fileCreationDate level=""7"">201301132210</fileCreationDate>
                               <fmtVersion level=""7"">3.0</fmtVersion>
                            </StartLot>";
    public class StartLot
    {
        [XmlElement("fileCreationDate")]
        public LevelAndValue FileCreationDate { get; set; }
        [XmlElement("fmtVersion")]
        public LevelAndValue FmtVersion { get; set; }
    }
    public class LevelAndValue
    {
        [XmlAttribute("level")]
        public string Level { get; set; }
        [XmlText]
        public string Value { get; set; }
    }
    public static void Test()
    {
        XmlSerializer xs = new XmlSerializer(typeof(StartLot));
        StartLot sl = (StartLot)xs.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(XML)));
        Console.WriteLine("FCD.L = {0}", sl.FileCreationDate.Level);
        Console.WriteLine("FCD.V = {0}", sl.FileCreationDate.Value);
        Console.WriteLine("FV.L = {0}", sl.FmtVersion.Level);
        Console.WriteLine("FV.V = {0}", sl.FmtVersion.Value);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I always find Linq To Xml easier to use

var xDoc = XDocument.Parse(xml); /* XDocument.Load(filename); */

var items = xDoc.Root.Descendants()
                .Select(e => new
                {
                    Name = e.Name.LocalName,
                    Level = e.Attribute("level").Value,
                    Value = e.Value
                })
                .ToList();

1 Comment

I'm just curious to know why you would replace a POCO-based serialization with manual XML generation/parsing (albeit via Xlinq)? Plus, your approach basically guts out any possibility of checking vs a schema.

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.