简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM