我使用的是VSTS2008 + C#+ .Net 3.0.我使用下面的代码来序列化XML,我的对象包含数组类型属性,我想在下面的预期结果中添加一个额外的元素'layer("MyInnerObjectProperties"元素层),我想将"MyInnerObjectProperties"元素作为父元素所有MyInnerObjectProperty元素的元素).有任何想法吗?
当前的序列化XML,
<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyObjectProperty>
<MyInnerObjectProperty>
<ObjectName>Foo Type</ObjectName>
</MyInnerObjectProperty>
<MyInnerObjectProperty>
<ObjectName>Goo Type</ObjectName>
</MyInnerObjectProperty>
</MyObjectProperty>
</MyClass>
Run Code Online (Sandbox Code Playgroud)
预期的序列化XML,
<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyObjectProperty>
<MyInnerObjectProperties>
<MyInnerObjectProperty>
<ObjectName>Foo Type</ObjectName>
</MyInnerObjectProperty>
<MyInnerObjectProperty>
<ObjectName>Goo Type</ObjectName>
</MyInnerObjectProperty>
</MyInnerObjectProperties>
</MyObjectProperty>
</MyClass>
Run Code Online (Sandbox Code Playgroud)
目前的代码,
public class MyClass
{
private MyObject[] _myObjectProperty;
[XmlElement(IsNullable=false)]
public MyObject[] MyObjectProperty
{
get
{
return _myObjectProperty;
}
set
{
_myObjectProperty = value;
}
}
}
public class MyObject
{
private MyInnerObject[] _myInnerObjectProperty;
[XmlElement(IsNullable = false)]
public MyInnerObject[] MyInnerObjectProperty …Run Code Online (Sandbox Code Playgroud) 我使用C#+ VSTS2008 + .Net 3.0来进行XML序列化.代码工作正常.下面是我的代码和当前序列化的XML结果.
现在我想在输出XML文件中添加两个附加层.这是我期望的XML结果.有什么简单的方法吗?我不确定NestingLevel是否可以帮助这样做.我想找到一种不会改变MyClass和MyObject结构的简单方法.
预期的XML序列化结果,
<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyObjectProperty>
<AdditionalLayer1>
<AdditionalLayer2>
<ObjectName>Foo</ObjectName>
</AdditionalLayer1>
</AdditionalLayer2>
</MyObjectProperty>
</MyClass>
Run Code Online (Sandbox Code Playgroud)
当前的XML序列化结果,
<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyObjectProperty>
<ObjectName>Foo</ObjectName>
</MyObjectProperty>
</MyClass>
Run Code Online (Sandbox Code Playgroud)
我目前的代码,
public class MyClass
{
public MyObject MyObjectProperty;
}
public class MyObject
{
public string ObjectName;
}
public class Program
{
static void Main(string[] args)
{
XmlSerializer s = new XmlSerializer(typeof(MyClass));
FileStream fs = new FileStream("foo.xml", FileMode.Create);
MyClass instance = new MyClass();
instance.MyObjectProperty = new MyObject();
instance.MyObjectProperty.ObjectName = "Foo";
s.Serialize(fs, …Run Code Online (Sandbox Code Playgroud)