考虑以下代码:
using System;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace XmlSerializationTest
{
[XmlType(Namespace = "http://www.test.com")]
public class Element
{
[XmlElement]
public int X;
}
[XmlRoot(Namespace = "http://www.test.com")]
public class Root
{
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public Element Element;
}
public static class Program
{
public static void Main(string[] args)
{
var root = new Root { Element = new Element { X = 1 } };
var xmlSerializer = new XmlSerializer(typeof(Root));
xmlSerializer.Serialize(Console.Out, root);
}
}
}
输出结果为:
<?xml version="1.0" encoding="ibm852"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com">
<Element xmlns="">
<X xmlns="http://www.test.com">1</X>
</Element>
</Root>
问题是为什么将Form属性设置为
XmlSchemaForm.Unqualified
会导致Element
元素的命名空间被设置为""
,即使它具有与根元素相同的命名空间的XmlTypeAttribute
属性?
WSCF.blue
工具生成这种代码(XmlSchemaForm.Unqualified
部分),它正在搞乱命名空间。