Home > Net >  With properties of serialized as XML node CDATA format
With properties of serialized as XML node CDATA format

Time:09-23

 & lt; Fulltext - file> 




Sample XML, for example,
Recently encountered in processing a NSTL standards related to the project need to handle the metadata related to NSTL standardization of XML CDATA format with attributes in the XML node, never find quick handling,
In dealing with the XML node without attributes CDATA format when I adopted the custom here a CDATA type:
 public class CData: IXmlSerializable 
{
Private string m_Value;

Public CData ()
{
}

Public CData (string p_Value)
{
M_Value=https://bbs.csdn.net/topics/p_Value;
}

Public string Value
{
The get
{
Return m_Value;
}
}

Public void ReadXml (XmlReader reader)
{
M_Value=(https://bbs.csdn.net/topics/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)
{
The return element (==null)? Null: element. M_Value;
}

Public static implicit operator CData (string text)
{
Return new CData (text);
}

}


In practical application of the node definition object is as follows:
 public class FullText 
{
[XmlElement (" object id ", the Namespace="http://jats.nlm.nih.gov/archiving/1.1/xsd")]
Public CData object_id {get; The set; }
}


The calling code:
 res. Record. Fulltext_file. Object_id="abc123"; 

Serialized as XML:
 & lt; Fulltext - file> 



But this way can only handle XML node without attributes, such as the top with attribute of XML nodes,
Want to ask next everybody if there is a quick way to define relevant object - object id, and serialization,
Such as:
 & lt; Fulltext - file> 



CodePudding user response:

The above custom CData type cannot be used as the basis of XMLText type, such as the type is defined as:
 public class object_ID 
{
[XmlElement (" pub - id - type ")]
Public string pub_id_type {get; The set; }

[XmlText]
Public CData value {get; The set; }

Public object_ID () {}

Public object_ID (string type, string value)
{
Enclosing pub_id_type=type;
This value=https://bbs.csdn.net/topics/value;
}
}

Eventually when serialized value prompts CData serialized failure type of the value

CodePudding user response:

The class CData isn't already custom 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=(https://bbs.csdn.net/topics/reader.ReadElementContentAsString);
}

Public void WriteXml (XmlWriter writer)
{
writer. WriteAttributeString (" pub - id - type ", m_idType);
writer. WriteCData (m_Value);
}
.
}

CodePudding user response:

refer to the second floor github_36000833 response:
class CData isn't already custom 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=(https://bbs.csdn.net/topics/reader.ReadElementContentAsString);
}

Public void WriteXml (XmlWriter writer)
{
writer. WriteAttributeString (" pub - id - type ", m_idType);
writer. WriteCData (m_Value);
}
.
}


Unknown type pub - id - this is the type of the above is just for example

CodePudding user response:

You don't pass a Dictionary also can?
Private Dictionary M_ attributes .
  •  Tags:  
  • C #
  • Related