繁体   English   中英

我如何使用System.Xml.Serialization注释此.net类型以序列化/反序列化以下XML /从以下XML

[英]How would I annotate this .net Type to serialise/deserialise to/from the following XML, using System.Xml.Serialization

方式:

public class Foo
{
    public string Bar { get; set; }
    public string Fred { get; set; }
}

实例

Foo x = new Foo { Bar = "Hi", Fred = "hey yourself" };

XML:

<Foo>
   <Bar>Hi</Bar>
   <John>                         <!-- Note extra layer -->
       <Fred>hey there</Fred>
   </John>
</Foo>

注意额外的John标记,但没有为John创建特殊类型。 如果我不能注释它,该如何以编程方式控制序列化过程。

尝试这个 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Runtime;
using System.Runtime.InteropServices;

namespace ConsoleApplication42
{
    class Program
    {

        static void Main(string[] args)
        {

            Foo x = new Foo { Bar = "Hi", Fred = "hey yourself" };
            //<Foo>
            //   <Bar>Hi</Bar>
            //   <John>                         <!-- Note extra layer -->
            //       <Fred>hey there</Fred>
            //   </John>
            //</Foo>

            XElement foo = new XElement("Foo", new object[] {
                new XElement("Bar", x.Bar),
                new XElement("John", new XElement("Fred", x.Fred))
            });

        }
    }
    public class Foo
    {
        public string Bar { get; set; }
        public string Fred { get; set; }
    }

}

暂无
暂无

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

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