The type:
public class Foo
{
public string Bar { get; set; }
public string Fred { get; set; }
}
The instance
Foo x = new Foo { Bar = "Hi", Fred = "hey yourself" };
The XML:
<Foo>
<Bar>Hi</Bar>
<John> <!-- Note extra layer -->
<Fred>hey there</Fred>
</John>
</Foo>
Note the extra John
tag, but without create a special type for John
. If I cannot annotate it, what how can I programmatically control the serialisation process.
Try this :
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; }
}
}
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.