我正在尝试在Xml序列化期间添加命名空间。到目前为止,我添加了一些但我无法使用Xml序列化程序添加自定义命名空间。
到目前为止,我已经实现了这个
<?xml version="1.0"?>
<manifest xmlns:xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2e" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xs" identifier="test" version="2">
<lom xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="test lom" xmlns="http://ltsc.ieee.org/xsd/LOM" />
<resource adlcp:scormtype="sco" />
</manifest>
这是我的代码
清单类
[Serializable]
[XmlRoot(ElementName = "manifest")]
public class Manifest
{
/// <summary>
/// manifest root node
/// </summary>
[XmlAttribute(AttributeName = "identifier")]
public string Identifier { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
//[XmlAttribute(AttributeName = "adlcp", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
//public string Adlcp = "http://www.adlnet.org/xsd/adlcp_rootv1p2";
[XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string SchemaLocation = "http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xs";
[XmlElement(ElementName = "lom", Namespace = "http://ltsc.ieee.org/xsd/LOM")]
public Lom Lom { get; set; }
[XmlElement("resource")]
public Resource Resource { get; set; }
}
Lom Class
public class Lom
{
[XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string SchemaLocation { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.imsproject.org/xsd/imscp_rootv1p1p2e")]
public string Xsi = "http://www.w3.org/2001/XMLSchema-instance";
}
资源类
public class Resource
{
[XmlAttribute(AttributeName = "scormtype", Namespace = "http://www.adlnet.org/xsd/adlcp_rootv1p2")]
public string ScormType { get; set; }
}
我的序列化功能就像这样
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
var xmlSerializer = new XmlSerializer(typeof(Manifest));
var ns = new XmlSerializerNamespaces();
ns.Add("xmlns", "http://www.imsproject.org/xsd/imscp_rootv1p1p2e");
ns.Add("adlcp", "http://www.adlnet.org/xsd/adlcp_rootv1p2");
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlSerializer.Serialize(fs, data, ns);
fs.Close();
}
我需要这样的输出。
<?xml version="1.0"?>
<manifest
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
identifier="test" version="2"
xmlns:adlcp="test"
xsi:schemaLocation="test location"
**xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"**>
<lom
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ltsc.ieee.org/xsd/LOM lom.xsd"
xmlns="http://ltsc.ieee.org/xsd/LOM" />
<resource adlcp:scormtype="sco"/>
</manifest>
我做错了什么?
答案 0 :(得分:1)
只需添加一个namspace序列化程序
var xmlSerializer = new XmlSerializer(typeof(Manifest), "http://www.w3.org/2001/XMLSchema-instance");
var ns = new XmlSerializerNamespaces();
ns.Add("xmlns", "http://www.w3.org/2001/XMLSchema-instance");
xmlSerializer.Serialize(fs, data, ns);
如果数据定义为
Manifest data = new Manifest()
{ Identifier = "test", Version = "2", Adlcp = "test", SchemaLocation = "test location" };
data.Resource = new Resource() { ScormType="sco" };
带有属性
[XmlAttribute(AttributeName = "adlcp", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Adlcp { get; set; }
您将在输出中获得xmlns:adlcp="test"
同样的事情
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Xsi = "http://www.w3.org/2001/XMLSchema-instance";
以及
ns.Add("adlcp", "http://www.imsproject.org/xsd/Manifest/Adlcp");
与
[XmlAttribute(AttributeName = "scormtype", Namespace = "http://www.imsproject.org/xsd/Manifest/Adlcp")]
public string ScormType { get; set; }
获取可能的adlcp:scormtype="sco"
也可以在根类中添加名称空间,例如
[XmlRoot(ElementName = "manifest", Namespace = "http://www.imsproject.org/xsd/Manifest")]
public class Manifest