我想在Xml序列化期間添加命名空間。到目前爲止,我已經添加了一些,但我無法使用Xml序列化程序添加自定義名稱空間。C#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>
這是我的代碼
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; }
}
洛美類
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";
}
資源類這樣
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();
}
public class Resource
{
[XmlAttribute(AttributeName = "scormtype", Namespace = "http://www.adlnet.org/xsd/adlcp_rootv1p2")]
public string ScormType { get; set; }
}
我的序列化功能,我需要這樣的輸出。
<?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>
我做錯了什麼?
如何實現所需的輸出? –
你確實需要'lom'子元素上重複的xsi命名空間聲明嗎?我認爲這部分是很難做到的,沒有自定義序列化,因爲聲明是不必要的(它已經在父代聲明瞭)。 – steve16351
你需要的最直接的事情是'[XmlRoot(ElementName =「manifest」,Namespace =「http://www.imsproject.org/xsd/imscp_rootv1p1p2」)]'? [XmlElement(「resource」)]''應該可能有一個明確的'Namespace' –