gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:09:46 25 4
gpt4 key购买 nike

类型:

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; }
}

}

关于c# - 我将如何使用 System.Xml.Serialization 注释此 .net 类型以序列化/反序列化到/从以下 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41755971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com