开发者

How to use .NET XML serialization to serialize an object to a single value

开发者 https://www.devze.com 2023-02-26 22:06 出处:网络
Assuming I have the following: [Serializable] public class Foo { public Bar bar { get; set; } public Ram ram { get;

Assuming I have the following:

[Serializable]
public class Foo
{
    public Bar bar
    {
        get;
        set;
    }

    public Ram ram
    {
        get;
        set;
    }
}

[Serializable]
public class Bar
{
    [XmlElement("barId")]
    public int Id
    {
        get;
        set;
    }
}

[Serializable]
public class Ram
{
    [XmlElement("ramId")]
    public int RamId
    {
        get;
        set;
    }
}

I would like to serialize to XML as:

<Foo>
    <barId>123</barId>
    <ramId>234</ramId>
</Foo>

What is the be开发者_如何学运维st way to do this?

I think I will have to create an intermediary class to serialize. Alternatives?


You mean like this?

using System.Text;
using System.Xml;
using System.Xml.Serialization ;

namespace ConsoleApplication11
{

  [XmlRoot("Foo")]
  public class Foo
  {
    public Foo()
    {
      bar = new Bar() ;
      ram = new Ram() ;
    }

    [XmlElement("barId")]
    public Bar bar { get; set; }

    [XmlElement("ramId")]
    public Ram ram { get; set; }

  }

  public class Bar
  {
    [XmlText]
    public int Id { get; set; }
  }

  public class Ram
  {
    [XmlText]
    public int RamId { get; set; }
  }

  class Program
  {

    static int Main( string[] argv )
    {
      XmlSerializer xml = new XmlSerializer( typeof(Foo) ) ;
      XmlWriterSettings settings = new XmlWriterSettings() ;

      settings.Indent = true ;
      settings.IndentChars = "  " ;
      settings.Encoding = new UnicodeEncoding( false , false ) ; // little-endian, omit byte order mark
      settings.OmitXmlDeclaration = true ;

      Foo instance = new Foo() ;
      instance.bar.Id = 1234 ;
      instance.ram.RamId = 9876 ;

      StringBuilder sb = new StringBuilder() ;
      using ( XmlWriter writer = XmlWriter.Create( sb , settings ) )
      {
        xml.Serialize(writer, instance ) ;
      }
      string xmlDoc = sb.ToString() ;

      Console.WriteLine(xmlDoc) ;

      return 0;
    }

  }

}


Use the XmlSerializer. This question is similar to yours and has some helpful answers: Using StringWriter for XML Serialization


I guess you could do something like this:

[Serializable]
public class Foo
{
private Bar _bar;

    public int BarID
    {
        get { return _bar.Id;}
        set 
        {
             if (_bar==null) _bar= new Bar();

             _bar.Id = id;
       }
    }

}

though i feel i should add that it sounds a bit misguided... why would you want to do this?


Mark Bar and Ram with attributes to prevent serialization, add public properties that expose the ids instead, have BarId's get return null if Bar is null, Bar.Id elsewise. Have the set either load existing Bar by id or create new one (according to your BL). Same for Ram.

0

精彩评论

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

关注公众号