views:

52

answers:

5

I have some XML which I would like to serialize into a class.

<MasterData>
<Data>
<SomeInnerData>
some inner data
</SomeInnerData>
</Data>
</MasterData> <MoreData>
<SubMoreData>moredate</SubMoreData>
</MoreDate>

[System.SerializableAttribute()]
public class MasterData
{
public string SomeInnnerData {get;set;}
public string SubMoreDate {get;set;}
}

How do I set the string member variables to serialize the appropriate data in the XML? My issue arises in that the element is not a child of the MasterData element.

A: 

With the normal .NET XMLSerializer class, public properties are serialized by default. You have to explicitly attribute properties not to be serialized:

[System.Xml.Serialization.XmlIgnoreAttribute]

Here's the documentation: XmlSerializer

...

Now that you've revised your question, the answer is that you will no longer be able to use the XMLSerializer class. (Or you will have to have some intermediary class which matches the structure of your XML, to facilitate the serialization and deserialization.) If you have a very specific XML structure that you want transform arbitrarily, use an XmlReader.

Eric Mickelsen
A: 

The XML example you provided is not valid XML thus you will not be able to directly serialize it. You need a root node for it work this way.

Such that:

<AllData>
    <MasterData>
        <Data>
           <SomeInnerData>
                  some inner data
          </SomeInnerData>
        </Data>
    </MasterData> 
        <MoreData>
             <SubMoreData>moredate</SubMoreData>
     </MoreDate>
<AllData>

[System.SerializableAttribute()]
public class AllData
{
public MasterData MasterData {get;set;}
public MoreData MoreData {get;set;}
}

[System.SerializableAttribute()]
public class Data
{
    public string SomeInnnerData {get;set;}
}

[System.SerializableAttribute()]
public class MasterData
{
    public string SomeInnnerData {get;set;}
}

[System.SerializableAttribute()]
public class MasterData
{
    public Data Data {get;set;}
}

 [System.SerializableAttribute()]
 public class MoreData
 {
    public string SubMoreDate {get;set;}
 }
Adam Driscoll
Is it possible to have the MoreDate in the MasterData and somehow tell the MasterData class that the node is located @ //RealMasterData/SubMoreData/
thenth
+2  A: 

The simplest way is to work backwards, get your class setup to serialize into the format you want, so that you can deserialize into it with ease.

Note: Your xml didn't validate, so I changed it to this for an example

<MasterData>
    <Data>
        <SomeInnerData>some inner data</SomeInnerData>
    </Data>
    <MoreData>
        <SubMoreData>moredate</SubMoreData>
    </MoreDate>
</MasterData> 

Currently, your problem is that you have Data and MoreData elements that don't map to anything

You'd need to create classes like

public class MasterData {
    public Data Data {get; set;}
    public MoreData Data {get; set;}
}

public class Data {
    public string SomeInnerData {get; set;}
}

public class MoreData {
    public string SubMoreData {get; set;}
}

You can have your properties named other things, and use the [XmlElement(ElementName="SubMoreData")] to map the property, to the correct Element.

Or, you could implement the IXmlSerializable interface, and write custom serialization code in a single class to map your class to xml however you want

Chad
A: 

Implement ixmlserializable for custom serialization

jgauffin
A: 

How about using XSL to transform from/to the XML format that your C# class directly serializes to?

Jerome