3

I've got three problems with some data that I'm serializing.

First off, it outputs <?xml version="1.0" encoding="utf-8"?> but the program that I'm loading it into only wants <?xml version="1.0"?>

Secondly, whenever the data is empty it will use shorthand for closing the tag (<z303-profile />) but the program that I'm loading it into won't accept that and requires <z303-profile></z303-profile>

Lastly, I have some data that I can't guarantee how long it will be so I have it in a List. I need each of the items to have their own heading of z305, but it outputs the name of the list that they're being held in first which messes everything up. It's being displayed as follows

    <z305List>
      <z305>
        ....
      </z305>
      <z305>
        ....
      </z305>
    </z305List>

with the list being stored as

[XmlArrayItem("z305")]
public List<LocalPatronInfo> z305List = new List<LocalPatronInfo>();

The code I'm using for serialization is as follows

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer xmls = new XmlSerializer(typeof(AllRecords));
TextWriter tw = new StreamWriter(richTextBoxWorkingDir.Text + "\\" + filename);
xmls.Serialize(tw, allRecords, ns);
tw.Close();
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Califer
  • 529
  • 9
  • 28
  • Got the third problem. It should have been [XMLElement("z305")] – Califer Aug 16 '10 at 22:27
  • You can use the `XmlWriter` and `XmlWriterSettings.OmitXmlDeclaration` if you don't need the XmlDeclaration (``) in the files you declare, not sure about the how the empty items are serialized though. Good luck! – Wil P Aug 16 '10 at 23:53
  • 2
    Is there tech support for the consumer, because unless they've got a c14n requirement (and if they did, they've the empty element thing the wrong way around), their not handling what you describe here is a bug, and it's the consumer that should be fixed. – Jon Hanna Aug 17 '10 at 01:22
  • 1
    @Jon +1 If the consumer can't handle the `encoding` attribute in the XML header, then it's not handling XML. – Mike Caron Aug 17 '10 at 03:47
  • 1
    There are some cases where you're allow to refuse based on the contents of the declaration, but that's not one of them. – Jon Hanna Aug 17 '10 at 07:40
  • You are making a mistake by "enabling" this consumer. They need to learn how to process XML. It's not like it's a new standard - it's been around for over a decade. What's their excuse for forcing everyone who deals with them to use a different standard? – John Saunders Aug 29 '10 at 21:48
  • 1
    I think it's funny that you think it's in my control. – Califer Aug 30 '10 at 15:44

1 Answers1

1

I think I've found the solution to first two problems with my third attempt. Advice was of course on this site (where else can it be? :-P) :

XmlSerializer uses XmlWriter to write XML. So when you make you own XMlWriter.. almost everything is possible. Please look at the following code:

public class XmlTextWriterFull : XmlTextWriter
{
    public XmlTextWriterFull(TextWriter sink) : base(sink) { }

    public override void WriteEndElement()
    {
        base.WriteFullEndElement();
    }

    public override void WriteStartDocument()
    {
        base.WriteRaw("<?xml version=\"1.0\"?>");
    }
}

public class temp
{
    public int a = 0;
    public List<int> x = new List<int>();
}

class Program
{
    static void Main(string[] args)
    {
        XmlTextWriterFull writer = new XmlTextWriterFull(Console.Out);

        XmlSerializer xs = new XmlSerializer(typeof(temp));
        xs.Serialize(writer,new temp());
        Console.ReadKey();
    }
}
Community
  • 1
  • 1
Bart
  • 2,167
  • 2
  • 15
  • 21
  • Very nice! This clears up all the issues but leaves everything on the same line instead of nicely indented. Once I changed the XmlTextWriterFull constructor to use "this.Formatting = Formatting.Indented;" the empty end elements ended up on a new line. Not a big deal for me since they give me a "null character" to work with. At least now I don't have to read the 250 meg file back in just to change one line anymore. Thanks again! – Califer Aug 30 '10 at 17:38
  • I'll look at it later, I think it also can be done in some way. Thanks for great question. Trying to solve it was really interesting and I also learn something new about XmlSerializer. Despite some minor disadvantages I like this method of generating XMLs a lot and use frequently. – Bart Aug 31 '10 at 06:30