XmlSerializer Deserializing Array/List of Multiple Objects at Root?

I think the issue is with your provided xml.

I think the issue is with your provided xml. Test app says List list = new List {new Foo {Val = "Data1"}, new Foo {Val = "Data2"}}; var s = new XmlSerializer(typeof(List)); StringBuilder sb = new StringBuilder(); XmlWriter wr = XmlWriter. Create(sb); s.

Serialize(wr, list); string ss = sb.ToString(); var s2 = new XmlSerializer(typeof(List)); StringReader sr = new StringReader(ss); List returnList = (List)s2. Deserialize(sr); And the XML should be Data1 Data2 If you can remove the inital line And minipulate the string into string s = " Data1 Data2"; var s2 = new XmlSerializer(typeof(List)); StringReader sr = new StringReader(s); List list = (List)s2. Deserialize(sr); That could work.

The problem is, the XML output is from a hardware device. So, I can't control the output. I need to parse it as is.

I was hoping there'd be an elegant way of doing it. =/ – Brian Chavez Dec 22 '09 at 12:10 Perhaps, every time you receive XML, you can wrap it with some root element, and then use XmlDocument to parse it. – ironic Dec 22 '09 at 12:51 Thanks, I've submitted a Firmware bug with the manufacture of the hardware device.

Hopefully, they'll fix the problem. – Brian Chavez Dec 22 '09 at 13:55.

That isn't valid Xml. There needs to be a core root element for it to work properly.

The XML output from a hardware device. I was hoping there was an elegant way to deal with it. – Brian Chavez Dec 22 '09 at 12:14.

This is not a valid xml so you can not deserialize it like a valid xml. You need some kind of hack to make this work. I'd suggest to insert at beginning of the xml and inserting at the end of xml.

Then you can deserialize it, since you cant make this change at xml side, do it in your code. String ss; // lets assume this holds your xml data in string. Ss.

Append(""); ss. Replace("", " ") var s2 = new XmlSerializer(typeof(List)); StringReader sr = new StringReader(ss); List returnList = (List)s2. Deserialize(sr); now this shall return you the correct list.

As the other posters say, this XML that the hardware device produces is not compatible to the way . NET serializes/deserializes object. Valid XML, and .

NET requires valid XML, has a root element. I suggest: either you modify your obtained XML to match the way astander presents in his xml code snippet. Or you write a simple xml parser for your file that deserializes the file like you need br, Marcel.

Technically, what you have there is not a well-formed XML document (which has exactly one root element), but rather an well-formed external parsed entity (which can have any number of elements not contained in other elements, as well as text not contained in any elements). Therefore, it should parse if your XML parser has an entry point for parsing EPEs rather than documents. You could also create a stub document which includes by reference your EPE and parse that document.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions