本文整理汇总了C#中System.Data.DataColumn.ConvertObjectToXml方法的典型用法代码示例。如果您正苦于以下问题:C# DataColumn.ConvertObjectToXml方法的具体用法?C# DataColumn.ConvertObjectToXml怎么用?C# DataColumn.ConvertObjectToXml使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.DataColumn
的用法示例。
在下文中一共展示了DataColumn.ConvertObjectToXml方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateColumn
private void GenerateColumn(DataRow row, DataColumn col, DataRowVersion version)
{
string columnValueAsString = null;
columnValueAsString = col.GetColumnValueAsString(row, version);
if (columnValueAsString == null)
{
if (col.ColumnMapping == MappingType.SimpleContent)
{
this._xmlw.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
}
}
else
{
bool flag;
string prefix = (col.Namespace.Length != 0) ? col.Prefix : string.Empty;
switch (col.ColumnMapping)
{
case MappingType.Element:
{
flag = true;
object obj2 = row[col, version];
if ((!col.IsCustomType || !col.IsValueCustomTypeInstance(obj2)) || typeof(IXmlSerializable).IsAssignableFrom(obj2.GetType()))
{
this._xmlw.WriteStartElement(prefix, col.EncodedColumnName, col.Namespace);
flag = false;
}
Type type = obj2.GetType();
if (col.IsCustomType)
{
if ((obj2 != DBNull.Value) && (!col.ImplementsINullable || !DataStorage.IsObjectSqlNull(obj2)))
{
if (col.IsValueCustomTypeInstance(obj2))
{
if (!flag && (obj2.GetType() != col.DataType))
{
this._xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", DataStorage.GetQualifiedName(type));
}
if (!flag)
{
col.ConvertObjectToXml(obj2, this._xmlw, null);
}
else
{
if (obj2.GetType() != col.DataType)
{
throw ExceptionBuilder.PolymorphismNotSupported(type.AssemblyQualifiedName);
}
XmlRootAttribute xmlAttrib = new XmlRootAttribute(col.EncodedColumnName) {
Namespace = col.Namespace
};
col.ConvertObjectToXml(obj2, this._xmlw, xmlAttrib);
}
}
else
{
if (((type == typeof(Type)) || (type == typeof(Guid))) || ((type == typeof(char)) || DataStorage.IsSqlType(type)))
{
this._xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", type.FullName);
}
else if (obj2 is Type)
{
this._xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", "Type");
}
else
{
string str3 = "xs:" + XmlTreeGen.XmlDataTypeName(type);
this._xmlw.WriteAttributeString("xsi", "type", "http://www.w3.org/2001/XMLSchema-instance", str3);
this._xmlw.WriteAttributeString("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
}
if (!DataStorage.IsSqlType(type))
{
this._xmlw.WriteString(col.ConvertObjectToXml(obj2));
}
else
{
col.ConvertObjectToXml(obj2, this._xmlw, null);
}
}
}
break;
}
if (((type == typeof(char)) || (type == typeof(string))) && XmlDataTreeWriter.PreserveSpace(columnValueAsString))
{
this._xmlw.WriteAttributeString("xml", "space", "http://www.w3.org/XML/1998/namespace", "preserve");
}
this._xmlw.WriteString(columnValueAsString);
break;
}
case MappingType.Attribute:
this._xmlw.WriteAttributeString(prefix, col.EncodedColumnName, col.Namespace, columnValueAsString);
return;
case MappingType.SimpleContent:
this._xmlw.WriteString(columnValueAsString);
return;
case MappingType.Hidden:
this._xmlw.WriteAttributeString("msdata", "hidden" + col.EncodedColumnName, "urn:schemas-microsoft-com:xml-msdata", columnValueAsString);
return;
//.........这里部分代码省略.........
示例2: GenerateColumn
private void GenerateColumn(DataRow row, DataColumn col, DataRowVersion version) {
string value = null;
value = col.GetColumnValueAsString(row, version); // this is useless for CTD
if (value == null) {
if (col.ColumnMapping == MappingType.SimpleContent)
_xmlw.WriteAttributeString(Keywords.XSI, Keywords.XSI_NIL, Keywords.XSINS, Keywords.TRUE);
return;
}
string colPrefix = (col.Namespace.Length != 0) ? col.Prefix : String.Empty;
switch (col.ColumnMapping) {
case MappingType.Attribute:
_xmlw.WriteAttributeString(colPrefix, col.EncodedColumnName, col.Namespace, value);
break;
case MappingType.Hidden:
_xmlw.WriteAttributeString(Keywords.MSD, "hidden"+col.EncodedColumnName, Keywords.MSDNS, value);
break;
case MappingType.SimpleContent:
_xmlw.WriteString(value);
break;
case MappingType.Element:
bool startElementSkipped = true;
object columnValue = row[col, version];
// if the object is built in type or if it implements IXMLSerializable, write the start Element, otherwise
//(if CDT and does not implement IXmlSerializable) skip it
if (!col.IsCustomType || !col.IsValueCustomTypeInstance(columnValue) ||(typeof(IXmlSerializable).IsAssignableFrom(columnValue.GetType()))) {
_xmlw.WriteStartElement( colPrefix, col.EncodedColumnName, col.Namespace);
startElementSkipped = false;
}
Type valuesType = columnValue.GetType();
if (!col.IsCustomType ) { // if column's type is built in type CLR or SQLType
if(valuesType == typeof(char) || valuesType == typeof(string)) {
if (XmlDataTreeWriter.PreserveSpace(value)) {
_xmlw.WriteAttributeString(Keywords.XML, Keywords.SPACE, Keywords.XML_XMLNS, Keywords.PRESERVE);
}
}
_xmlw.WriteString(value);
}
else { // Columns type is CDT
if ((columnValue != DBNull.Value) && (!col.ImplementsINullable || !DataStorage.IsObjectSqlNull(columnValue))){
if (col.IsValueCustomTypeInstance(columnValue)/* && valuesType != typeof(Type)*/) {// value is also CDT
// if SkippedElement, ie does not implement IXMLSerializable: so No Polymorphysm Support.
if (!startElementSkipped && columnValue.GetType() != col.DataType) {
_xmlw.WriteAttributeString(Keywords.MSD, Keywords.MSD_INSTANCETYPE, Keywords.MSDNS, DataStorage.GetQualifiedName(valuesType));
}
if (!startElementSkipped) {
col.ConvertObjectToXml(columnValue, _xmlw, null); // XmlRootAttribute MUST be passed null
}
else{
// this guy does not implement IXmlSerializable, so we need to handle serialization via XmlSerializer
if (columnValue.GetType() != col.DataType) { // throw if polymorphism; not supported
throw ExceptionBuilder.PolymorphismNotSupported(valuesType.AssemblyQualifiedName);
}
// therefore we are skipping the start element, but by passing XmlRootAttribute with the same name as
// we open the start element (column's name), XmlSerializer will open and close it for us
XmlRootAttribute xmlAttrib = new XmlRootAttribute(col.EncodedColumnName);
xmlAttrib.Namespace = col.Namespace;
col.ConvertObjectToXml(columnValue, _xmlw, xmlAttrib);
}
}
else { // value is built in CLR type (eg: string, int etc.)
// these basic clr types do not have direct xsd type mappings
if (valuesType == typeof(Type) || valuesType == typeof(Guid)|| valuesType == typeof(Char) ||
DataStorage.IsSqlType(valuesType) ) { // if unmapped type or SQL type write msdata:Datatype=typeofinstance
_xmlw.WriteAttributeString(Keywords.MSD, Keywords.MSD_INSTANCETYPE, Keywords.MSDNS, valuesType.FullName);
}
else if (columnValue is Type) {
_xmlw.WriteAttributeString(Keywords.MSD, Keywords.MSD_INSTANCETYPE, Keywords.MSDNS, Keywords.TYPEINSTANCE);
}
else {
string xsdTypeName = Keywords.XSD_PREFIXCOLON+ XmlTreeGen.XmlDataTypeName(valuesType);
_xmlw.WriteAttributeString(Keywords.XSI, Keywords.TYPE, Keywords.XSINS, xsdTypeName);
_xmlw.WriteAttributeString (Keywords.XMLNS_XSD, Keywords.XSDNS);
}
if (!DataStorage.IsSqlType(valuesType)) {
_xmlw.WriteString(col.ConvertObjectToXml(columnValue));
}
else {
col.ConvertObjectToXml(columnValue, _xmlw, null);
}
}
}
}
if (!startElementSkipped) {
_xmlw.WriteEndElement();
}
break;
}
}
示例3: OnColumnValueChanged
private void OnColumnValueChanged(DataRow row, DataColumn col, XmlBoundElement rowElement)
{
if (IsNotMapped(col))
{
goto lblDoNestedRelationSync;
}
object value = row[col];
if (col.ColumnMapping == MappingType.SimpleContent && Convert.IsDBNull(value) && !rowElement.IsFoliated)
{
ForceFoliation(rowElement, ElementState.WeakFoliation);
}
else
{
// no need to sync if not foliated
if (!IsFoliated(rowElement))
{
#if DEBUG
// If the new value is null, we should be already foliated if there is a DataPointer that points to the column
// (see OnRowChanging, case DataRowAction.Change)
if (Convert.IsDBNull(row[col, DataRowVersion.Current]))
{
try
{
if (_pointers.Count > 0)
{
object pointer = null;
foreach (DictionaryEntry entry in _pointers)
{
pointer = entry.Value;
Debug.Assert((pointer != null) && !((IXmlDataVirtualNode)pointer).IsOnColumn(col));
}
}
}
catch (Exception e) when (Data.Common.ADP.IsCatchableExceptionType(e))
{
// We may get an exception if we are in foreach and a new pointer has been added to this.pointers. When this happens, we will skip this check and ignore the exceptions
}
}
#endif
goto lblDoNestedRelationSync;
}
}
if (IsTextOnly(col))
{
if (Convert.IsDBNull(value))
{
value = string.Empty;
//make sure that rowElement has Attribute xsi:nil and its value is true
XmlAttribute attr = rowElement.GetAttributeNode(XSI_NIL);
if (attr == null)
{
attr = CreateAttribute(XSI, Keywords.XSI_NIL, Keywords.XSINS);
attr.Value = Keywords.TRUE;
rowElement.SetAttributeNode(attr);
_bHasXSINIL = true;
}
else
attr.Value = Keywords.TRUE;
}
else
{
//make sure that if rowElement has Attribute xsi:nil, its value is false
XmlAttribute attr = rowElement.GetAttributeNode(XSI_NIL);
if (attr != null)
attr.Value = Keywords.FALSE;
}
ReplaceInitialChildText(rowElement, col.ConvertObjectToXml(value));
goto lblDoNestedRelationSync;
}
// update the attribute that maps to the column
bool fFound = false;
// Find the field node and set it's value
if (col.ColumnMapping == MappingType.Attribute)
{
foreach (XmlAttribute attr in rowElement.Attributes)
{
if (attr.LocalName == col.EncodedColumnName && attr.NamespaceURI == col.Namespace)
{
if (Convert.IsDBNull(value))
{
attr.OwnerElement.Attributes.Remove(attr);
}
else
{
attr.Value = col.ConvertObjectToXml(value);
}
fFound = true;
break;
}
}
// create new attribute if we didn't find one.
if (!fFound && !Convert.IsDBNull(value))
{
rowElement.SetAttribute(col.EncodedColumnName, col.Namespace, col.ConvertObjectToXml(value));
//.........这里部分代码省略.........
示例4: HandleColumn
internal XmlElement HandleColumn(DataColumn col, XmlDocument dc, XmlElement schema, bool fWriteOrdinal) {
XmlElement root;
int minOccurs;
Debug.Assert(col.ColumnMapping != MappingType.SimpleContent , "Illegal state");
String refString = (col.ColumnMapping != MappingType.Element) ? Keywords.XSD_ATTRIBUTE : Keywords.XSD_ELEMENT;
root = dc.CreateElement(Keywords.XSD_PREFIX, refString, Keywords.XSDNS);
// First add any attributes.
root.SetAttribute( Keywords.NAME, col.EncodedColumnName);
if (col.Namespace.Length == 0) {
DataTable _table = col.Table;
// We need to travese the hirerarchy to find the targetnamepace
string tgNamespace = FindTargetNamespace(_table);
if (col.Namespace != tgNamespace) {
root.SetAttribute( Keywords.FORM, Keywords.UNQUALIFIED);
}
}
if (col.GetType() != typeof(DataColumn))
AddXdoProperties(col, root, dc);
else
AddColumnProperties(col, root);
AddExtendedProperties(col.extendedProperties, root);
HandleColumnType(col, dc, root, schema);
if (col.ColumnMapping == MappingType.Hidden) { // CDT / UDT can not be mapped to Hidden column
if (!col.AllowDBNull)
root.SetAttribute(Keywords.MSD_ALLOWDBNULL, Keywords.MSDNS, Keywords.FALSE);
if (!col.DefaultValueIsNull)
if (col.DataType == typeof(bool))
root.SetAttribute(Keywords.MSD_DEFAULTVALUE, Keywords.MSDNS, (bool)(col.DefaultValue)? Keywords.TRUE : Keywords.FALSE);
else
{
XmlTreeGen.ValidateColumnMapping(col.DataType);
root.SetAttribute(Keywords.MSD_DEFAULTVALUE, Keywords.MSDNS, col.ConvertObjectToXml(col.DefaultValue));
}
}
if ((!col.DefaultValueIsNull)&& (col.ColumnMapping != MappingType.Hidden)){
XmlTreeGen.ValidateColumnMapping(col.DataType);
if (col.ColumnMapping == MappingType.Attribute && !col.AllowDBNull ) {
if (col.DataType == typeof(bool)) {
root.SetAttribute(Keywords.MSD_DEFAULTVALUE, Keywords.MSDNS, (bool)(col.DefaultValue)? Keywords.TRUE : Keywords.FALSE);
}
else { // CDT / UDT columns cn not be mapped to Attribute also
root.SetAttribute(Keywords.MSD_DEFAULTVALUE, Keywords.MSDNS, col.ConvertObjectToXml(col.DefaultValue));
}
}
else { // Element Column : need to handle CDT
if (col.DataType == typeof(bool)) {
root.SetAttribute(Keywords.DEFAULT, (bool)(col.DefaultValue)? Keywords.TRUE : Keywords.FALSE);
}
else {
if (!col.IsCustomType) { // built in type
root.SetAttribute(Keywords.DEFAULT, col.ConvertObjectToXml(col.DefaultValue));
}
else { // UDT column
}
}
}
}
if (schFormat == SchemaFormat.Remoting)
root.SetAttribute( Keywords.TARGETNAMESPACE, Keywords.MSDNS, col.Namespace);
else {
if ((col.Namespace != (col.Table.TypeName.IsEmpty ? col.Table.Namespace : col.Table.TypeName.Namespace)) && (col.Namespace.Length != 0))
{
XmlElement schNode = GetSchema(col.Namespace);
if (FindTypeNode(schNode, col.EncodedColumnName) == null)
schNode.AppendChild(root);
root = _dc.CreateElement(Keywords.XSD_PREFIX, refString, Keywords.XSDNS);
root.SetAttribute( Keywords.REF, prefixes[ col.Namespace]+":"+ col.EncodedColumnName);
if (col.Table.Namespace!=_ds.Namespace) {
string prefix = (string)prefixes[col.Namespace];
XmlElement tNode = GetSchema(col.Table.Namespace);
}
}
}
minOccurs = (col.AllowDBNull) ? 0 : 1;
// [....] 2001 change
if (col.ColumnMapping == MappingType.Attribute && minOccurs != 0)
root.SetAttribute(Keywords.USE, Keywords.REQUIRED);
if (col.ColumnMapping == MappingType.Hidden) {
root.SetAttribute(Keywords.USE, Keywords.PROHIBITED);
}
else
//.........这里部分代码省略.........
示例5: HandleColumn
internal XmlElement HandleColumn(DataColumn col, XmlDocument dc, XmlElement schema, bool fWriteOrdinal)
{
string localName = (col.ColumnMapping != MappingType.Element) ? "attribute" : "element";
XmlElement root = dc.CreateElement("xs", localName, "http://www.w3.org/2001/XMLSchema");
root.SetAttribute("name", col.EncodedColumnName);
if (col.Namespace.Length == 0)
{
DataTable table = col.Table;
string str2 = this.FindTargetNamespace(table);
if (col.Namespace != str2)
{
root.SetAttribute("form", "unqualified");
}
}
if (col.GetType() != typeof(DataColumn))
{
this.AddXdoProperties(col, root, dc);
}
else
{
this.AddColumnProperties(col, root);
}
AddExtendedProperties(col.extendedProperties, root);
this.HandleColumnType(col, dc, root, schema);
if (col.ColumnMapping == MappingType.Hidden)
{
if (!col.AllowDBNull)
{
root.SetAttribute("AllowDBNull", "urn:schemas-microsoft-com:xml-msdata", "false");
}
if (!col.DefaultValueIsNull)
{
if (col.DataType == typeof(bool))
{
root.SetAttribute("DefaultValue", "urn:schemas-microsoft-com:xml-msdata", ((bool) col.DefaultValue) ? "true" : "false");
}
else
{
ValidateColumnMapping(col.DataType);
root.SetAttribute("DefaultValue", "urn:schemas-microsoft-com:xml-msdata", col.ConvertObjectToXml(col.DefaultValue));
}
}
}
if (!col.DefaultValueIsNull && (col.ColumnMapping != MappingType.Hidden))
{
ValidateColumnMapping(col.DataType);
if ((col.ColumnMapping == MappingType.Attribute) && !col.AllowDBNull)
{
if (col.DataType == typeof(bool))
{
root.SetAttribute("DefaultValue", "urn:schemas-microsoft-com:xml-msdata", ((bool) col.DefaultValue) ? "true" : "false");
}
else
{
root.SetAttribute("DefaultValue", "urn:schemas-microsoft-com:xml-msdata", col.ConvertObjectToXml(col.DefaultValue));
}
}
else if (col.DataType == typeof(bool))
{
root.SetAttribute("default", ((bool) col.DefaultValue) ? "true" : "false");
}
else if (!col.IsCustomType)
{
root.SetAttribute("default", col.ConvertObjectToXml(col.DefaultValue));
}
}
if (this.schFormat == SchemaFormat.Remoting)
{
root.SetAttribute("targetNamespace", "urn:schemas-microsoft-com:xml-msdata", col.Namespace);
}
else if ((col.Namespace != (col.Table.TypeName.IsEmpty ? col.Table.Namespace : col.Table.TypeName.Namespace)) && (col.Namespace.Length != 0))
{
XmlElement node = this.GetSchema(col.Namespace);
if (this.FindTypeNode(node, col.EncodedColumnName) == null)
{
node.AppendChild(root);
}
root = this._dc.CreateElement("xs", localName, "http://www.w3.org/2001/XMLSchema");
root.SetAttribute("ref", this.prefixes[col.Namespace] + ":" + col.EncodedColumnName);
if (col.Table.Namespace != this._ds.Namespace)
{
string text1 = (string) this.prefixes[col.Namespace];
this.GetSchema(col.Table.Namespace);
}
}
int num = col.AllowDBNull ? 0 : 1;
if ((col.ColumnMapping == MappingType.Attribute) && (num != 0))
{
root.SetAttribute("use", "required");
}
if (col.ColumnMapping == MappingType.Hidden)
{
root.SetAttribute("use", "prohibited");
}
else if ((col.ColumnMapping != MappingType.Attribute) && (num != 1))
{
root.SetAttribute("minOccurs", num.ToString(CultureInfo.InvariantCulture));
}
if ((col.ColumnMapping == MappingType.Element) && fWriteOrdinal)
{
//.........这里部分代码省略.........