开发者

C# XML Serialization of Attribute

开发者 https://www.devze.com 2023-01-14 03:27 出处:网络
I have some XML which I would like to serialize into a class. <MasterData> <Data> <SomeInnerData>

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

<MasterData>  
  <Data>  
    <SomeInnerData>  
      s开发者_Go百科ome inner data  
    </SomeInnerData>  
  </Data>  
</MasterData>
<MoreData>  
  <SubMoreData>moredate</SubMoreData>  
</MoreDate>  

and

[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.


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


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;}
 }


Implement ixmlserializable for custom serialization


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.


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

0

精彩评论

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