我想使用xmlserializer更改xml命名空间。我浏览网页没有结果。序列化器的结果是:
<?xml version="1.0" encoding="utf-8"?>
<OLPData mlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Resource>
</Resource>
</OLPData>
我的序列化器看起来像这样:
XmlSerializer s = new XmlSerializer(typeof(List<Resource>), new XmlRootAttribute("OLPData"));
TextWriter t = new StreamWriter("code.xml");
List<Resource> xml = new List<Resource>() { general }s.Serialize(t, xml);
t.Close();
所以我有一个Ressource类型的列表,一般作为源。我试图补充一下:
public partial class Resource
{
[XmlAttribute(AttributeName = "noNamespaceSchemaLocation", Namespace = XmlSchema.InstanceNamespace)]
public string attr = @"C:\Program Files\Dassault Systemes\B19\intel_a\startup\Olp\XSchemas\Upload.xsd";
}
在xml类结构中,但它显然将noNamespaceSchemaLocation添加到ressource而不是OLDData。
<?xml version="1.0" encoding="utf-8"?>
<OLPData mlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Resource xsi:noNamespaceSchemaLocation = "C:\Program Files\Dassault Systemes\B19\intel_a\startup\Olp\XSchemas\Upload.xsd">
</Resource>
</OLPData>
我想要的是这个结果:
<?xml version="1.0" encoding="utf-8"?>
<OLPData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Program Files\Dassault Systemes\B19\intel_a\startup\Olp\XSchemas\Upload.xsd">
<Resource>
</Resource>
</OLPDate>
有什么建议吗?
答案 0 :(得分:2)
我得到的第一个想法,似乎工作正常:( 修改每条评论)
void Main()
{
var s = new XmlSerializer(typeof(OlpData));
using (var t = new StreamWriter("code.xml"))
{
var xml = new OlpData { Resources = new[] { WriteGeneral() } };
s.Serialize(t, xml);
}
}
[XmlRoot("OLPData")]
public partial class OlpData
{
[XmlAttribute(AttributeName = "noNamespaceSchemaLocation", Namespace = XmlSchema.InstanceNamespace)]
public string attr = @"C:\Program Files\Dassault Systemes\B19\intel_a\startup\Olp\XSchemas\Upload.xsd";
[XmlElement("Resource")]
public Resource[] Resources;
}
public partial class Resource
{
}