<fulltext-file>
<jats:object-id pub-id-type="roid">RO201909193412970ZX.pdf</jats:object-id>
<jats:object-id pub-id-type="uri"><![CDATA[https://ttu-ir.tdl.org/handle/2346/84945]]></jats:object-id>
<jats:object-id pub-id-type="download_uri"><![CDATA[https://ttu-ir.tdl.org/bitstream/handle/2346/84945/1/ICES-2019-178.pdf?sequence=1&isAllowed=y]]></jats:object-id>
</fulltext-file>
XML樣例如上。
最近在處理一個NSTL標準相關的專案遇到需要把相關的元資料處理為NSTL標準化XML 其中的XML節點里有帶屬性的CDATA格式節點,始終未找到快捷的處理方式。
在處理不帶屬性的XML節點CDATA格式的時候 我這邊采用了自定義一個CData型別:
public class CData : IXmlSerializable
{
private string m_Value;
public CData()
{
}
public CData(string p_Value)
{
m_Value = p_Value;
}
public string Value
{
get
{
return m_Value;
}
}
public void ReadXml(XmlReader reader)
{
m_Value = reader.ReadElementContentAsString();
}
public void WriteXml(XmlWriter writer)
{
writer.WriteCData(m_Value);
}
public XmlSchema GetSchema()
{
return (null);
}
public override string ToString()
{
return m_Value;
}
public static implicit operator string(CData element)
{
return (element == null) ? null : element.m_Value;
}
public static implicit operator CData(string text)
{
return new CData(text);
}
}
在實際應用的時候相關節點定義物件如下:
public class FullText
{
[XmlElement("object-id", Namespace = "http://jats.nlm.nih.gov/archiving/1.1/xsd")]
public CData object_id { get; set; }
}
呼叫代碼:
res.record.fulltext_file.object_id = "abc123";
序列化為XML:
<fulltext-file>
<jats:object-id><![CDATA[abc123]]></jats:object-id>
</fulltext-file>
但這種方式只能處理不帶屬性的XML節點,如最上面帶屬性的xml節點,
想請教下各位是否有快捷的方式定義相關的object-id物件,并序列化。
如:
<fulltext-file>
<jats:object-id pub-id-type="roid">RO201909193412970ZX.pdf</jats:object-id>
<jats:object-id pub-id-type="uri"><![CDATA[https://ttu-ir.tdl.org/handle/2346/84945]]></jats:object-id>
<jats:object-id pub-id-type="download_uri"><![CDATA[https://ttu-ir.tdl.org/bitstream/handle/2346/84945/1/ICES-2019-178.pdf?sequence=1&isAllowed=y]]></jats:object-id>
</fulltext-file>
uj5u.com熱心網友回復:
上面自定義的CData型別不能作為XMLText的基礎型別,如型別定義為:public class object_ID
{
[XmlElement("pub-id-type")]
public string pub_id_type { get; set; }
[XmlText]
public CData value { get; set; }
public object_ID() { }
public object_ID(string type, string value)
{
this.pub_id_type = type;
this.value = value;
}
}
最終在序列化的時候 value出會提示CData型別的value序列化失敗
uj5u.com熱心網友回復:
class CData 不是已經自定義了 IXmlSerializable?public class CData : IXmlSerializable
{
private string m_Value;
private string m_idType;
public void ReadXml(XmlReader reader)
{
m_idType = reader.GetAttribute("pub-id-type");
m_Value = reader.ReadElementContentAsString();
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("pub-id-type", m_idType);
writer.WriteCData(m_Value);
}
...
}
uj5u.com熱心網友回復:
型別 pub-id-type 這個是未知的 上面只是舉例
uj5u.com熱心網友回復:
你傳一個Dictionary不也可以?private Dictionary<string,string> m_attributes;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/76303.html
標籤:C#
