开发者

Using the .Net XmlSerializer, is there any way to omit the root-level tag?

I need to serialize some Xml according to a web-kind-of-service specification. Due to some reasons I have created a few different classes to encapsulate the information for my application. These classes does not match directly to the xml schema. See below.

<TopLevel>
    <InfoString>Value</InfoString>
    <InfoInt>242</InfoInt>
    <Etcetera>Value2</Etc开发者_C百科etera>
</TopLevel

While my classes are something like:

public class Info
    -InfoString
    -InfoInt
public class Etcetera
    -EtceteraValue

This yields incorrect Xml, as there is an element Info containing the InfoStrings, and the Etcetera element contains EtceteraValue.

I implement IXmlSerializable, but the XmlSerializable still writes the root-level of my classes. Any ideas?

For clarity: I am trying to serialize multiple classes into the same document, on the same level.


I don't believe this is possible, since this way the serializer could produce xml which cannot be deserialized unambiguously, if more than one class has an attribute of the same name.

Either you do the serialization completely on your own or use XSLT to transform the output into the xml format you need.

What I did in the past is to create transfer-classes which have the right structure to serialize exactly into the format I want. You only need to fill the values from your original object into the transfer object.

EDIT Another idea:

You could query all the data in the format you want from your object using a LINQ-Expression. After that, you can use LINQ to XML to serialize the result into xml. I've never done this before, if you have luck with that please let me know.


If you need fine control over your xml output and join completely seperate classes of objects into a single XML structured differently you might consider writing out your XML manually using the XmlWriter, XDocument or XmlDocument.

I do not know how to join these classes using mere attribute markups to control serialization.


Either you:

  1. Flatten the classes so the serialization matches your desired schema.
  2. Create a new flattened class, and use some mapping framework to map between your structured classes and the flat serailizable class, like Value Injecter
  3. Do not modify the type definitions, and try to modify the XML using XSLT


   public class RootClass
    {
        public Info info { get; set; }
        public Etcetera etc { get; set; }
    }

    public class Info
    {
        [XmlElement]
        public string InfoStr { get; set; }
        [XmlElement]
        public int InfoInt { get; set; }
    }

    public class Etcetera
    {
        [XmlElement]
        public string EtceteraValue { get; set; }
    }

And then serialize the RootClass

This will give you:

<?xml version="1.0"?>
<RootClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<info>
  <InfoStr>asdf</InfoStr>
  <InfoInt>1</InfoInt>
</info>
<etc>
  <EtceteraValue>etc</EtceteraValue>
</etc>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜