简体   繁体   English

如何使用.NET XML序列化将对象序列化为单个值

[英]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: 我想序列化为XML为:

<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. 使用XmlSerializer。 This question is similar to yours and has some helpful answers: Using StringWriter for XML Serialization 这个问题与您的问题相似,并且有一些有用的答案: 使用StringWriter进行XML序列化

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. 将属性标记为Bar和Ram以防止序列化,添加公开属性以代替ID,如果Bar为null,则使BarId的get返回null,否则返回Bar.Id。 Have the set either load existing Bar by id or create new one (according to your BL). 让集合按ID加载现有的Bar或创建一个新的Bar(根据您的BL)。 Same for Ram. Ram也一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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