Element name of serialized List<t></t>
-
I'm starting to pull my hair out and hope somebody can help stop me from becoming bald! I have a Class - LookupItem that uses XmlRoot attribute so that when serialized individually outputs under element named Item. Now if I have a List when I serialize it the lookup item class always serializes as LookupItem and not Item. What I'm wanting to see is: <Clean> <Item> .... </Item> </Clean> but all I get is: <Clean> <LookupItem> .... </LookupItem> </Clean> The code I'm currently using to do the serialization is below, but I have tried doing serialization with extra types, XmlAttributeOverrides and still cannot get the output correct.
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlAttributeOverrides xao = new XmlAttributeOverrides(); XmlRootAttribute xr = new XmlRootAttribute("Clean"); XmlSerializer s = new XmlSerializer(typeof(LookupItemList)); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; StringWriter output = new StringWriter(); XmlWriter writer = XmlWriter.Create(output, settings); //TextWriter w = new StreamWriter(); s.Serialize(writer, Clean, ns); writer.Close(); output.ToString();
Can anybody help me at all? -
I'm starting to pull my hair out and hope somebody can help stop me from becoming bald! I have a Class - LookupItem that uses XmlRoot attribute so that when serialized individually outputs under element named Item. Now if I have a List when I serialize it the lookup item class always serializes as LookupItem and not Item. What I'm wanting to see is: <Clean> <Item> .... </Item> </Clean> but all I get is: <Clean> <LookupItem> .... </LookupItem> </Clean> The code I'm currently using to do the serialization is below, but I have tried doing serialization with extra types, XmlAttributeOverrides and still cannot get the output correct.
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlAttributeOverrides xao = new XmlAttributeOverrides(); XmlRootAttribute xr = new XmlRootAttribute("Clean"); XmlSerializer s = new XmlSerializer(typeof(LookupItemList)); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; StringWriter output = new StringWriter(); XmlWriter writer = XmlWriter.Create(output, settings); //TextWriter w = new StreamWriter(); s.Serialize(writer, Clean, ns); writer.Close(); output.ToString();
Can anybody help me at all?I think you need to apply the XmlRootAttribute to the LookupItemList class. So Something like:
[XmlRoot("Clean")]
public class LookupItemList ...Then you would need to do the same thing with the LookupItem class, but use XmlElementAttribute to define the name as "Item". FYI...In your code example above you don't use xr or xao anywhere, are you sure this is the code you are using?
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
-
I think you need to apply the XmlRootAttribute to the LookupItemList class. So Something like:
[XmlRoot("Clean")]
public class LookupItemList ...Then you would need to do the same thing with the LookupItem class, but use XmlElementAttribute to define the name as "Item". FYI...In your code example above you don't use xr or xao anywhere, are you sure this is the code you are using?
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
Sorry Tom, Just realised code posted not quite correct I'm using:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer s = new XmlSerializer(typeof(LookupItem)); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; StringWriter output = new StringWriter(); XmlWriter writer = XmlWriter.Create(output, settings); //TextWriter w = new StreamWriter(); s.Serialize(writer, toSerialize, ns); writer.Close(); return output.ToString();
The root element won't help unfortunately since I'm trying to serialise a List, even if I did create a specialist list based on type T all I get output is the correct root element, the issue is the items in the list being output with the correct element name as specified by myself using XmlRoot("Item") on the class that is stored in the list. -
Sorry Tom, Just realised code posted not quite correct I'm using:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer s = new XmlSerializer(typeof(LookupItem)); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; StringWriter output = new StringWriter(); XmlWriter writer = XmlWriter.Create(output, settings); //TextWriter w = new StreamWriter(); s.Serialize(writer, toSerialize, ns); writer.Close(); return output.ToString();
The root element won't help unfortunately since I'm trying to serialise a List, even if I did create a specialist list based on type T all I get output is the correct root element, the issue is the items in the list being output with the correct element name as specified by myself using XmlRoot("Item") on the class that is stored in the list.For the LookupItem class you should use the XmlElementAttribute (I think). So something like this:
[XmlElement("Item")]
public class LookupItem ...The XmlRootAttribute is only good for the root item in the XML doc (of which there is only one). Let me know if this doesn't work, though.
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
-
For the LookupItem class you should use the XmlElementAttribute (I think). So something like this:
[XmlElement("Item")]
public class LookupItem ...The XmlRootAttribute is only good for the root item in the XML doc (of which there is only one). Let me know if this doesn't work, though.
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
XmlElement can not be used on a class only property, index, field or param. The XmlRoot attribute simply tells the serializer that for this particular class to use a specific name for the node, it doesn't directly relate to the root element of the doc. Cheers Nathan
-
XmlElement can not be used on a class only property, index, field or param. The XmlRoot attribute simply tells the serializer that for this particular class to use a specific name for the node, it doesn't directly relate to the root element of the doc. Cheers Nathan
Sorry, my memory isn't what is used to be ;-) Here is the code that should do what you want...
[XmlType(TypeName = "Item")]
public class LookupItem {
// TODO
}[XmlRoot("Clean")]
public class LookupItemList : List<LookupItem> {
// TODO
}I'm assuming you are using a generic List and that you are serializing the List as the root object.
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
-
Sorry, my memory isn't what is used to be ;-) Here is the code that should do what you want...
[XmlType(TypeName = "Item")]
public class LookupItem {
// TODO
}[XmlRoot("Clean")]
public class LookupItemList : List<LookupItem> {
// TODO
}I'm assuming you are using a generic List and that you are serializing the List as the root object.
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
XmlType - of course! Got so hung up on the XmlRoot that I completely forgot about that. Got it all serializing lovely now, managed it without a specific class by simply providing a XmlRoot attribute to the serializer. Thanks Tom