本文介绍了Datatable.WriteXml - 如何美元的元素名称p $ pserve空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序添加的功能,其中一个数据表转化为 XML ,像这样:

  dtResults.WriteXml(文件名);
 

_x0020 _ 将被加入,而不是输出的XML文档中的空格。

是否有可能生成XML文件,而 _x0020 _ code?即可以在XML文件中使用的方法或类似的产生,居然有空间preserved?

这是DataGrid的:

这是生成的XML:

 <客户>
    < Customer_x0020_Name>肖恩< / Customer_x0020_Name>
  < /客户>
  <客户>
    < Customer_x0020_Name>约翰< / Customer_x0020_Name>
  < /客户>
  <客户>
    < Customer_x0020_Name>莎拉< / Customer_x0020_Name>
  < /客户>
  <客户>
    < Customer_x0020_Name>马克< / Customer_x0020_Name>
  < /客户>
  <客户>
    < Customer_x0020_Name>诺曼< / Customer_x0020_Name>
  < /客户>
 

解决方案

你列的名称包含空格。 XML元素名称不能包含空格。用空格在XML中的元素名称从属性名称分开,比如:

 <的ElementName ATTRIBUTE1 =值/>
 

DataTable.WriteXml 方法尝试写出来的XML文件以一致的方式,以便其他数据表对象稍后可用于加载XML并获得尽可能接近原始尽可能的精确副本。因此,它取代非法字符与十六进制值,以便非法字符的翻译不会丢失。

所以,如果你想将它不同写到XML,你需要:

  • 更改列的名称数据表,以便它不包含空格
  • 在手动输出自己使用的XML 的XDocument 的XmlDocument 的XmlWriter 的XmlSerializer 和格式化但输出你的愿望
  • 输出XML,你现在要做的,但运行的XSLT脚本,就可以以固定的格式

I've added functionality in my application where a DataTable is converted into XML, like so:

dtResults.WriteXml(fileName);

_x0020_ is being added instead of spaces within the outputted XML document.

Is it possible to generate XML files without the _x0020_ code? i.e. can an XML file be generated using that method or similar, and actually have the spaces preserved?

This is the DataGrid:

This is the resulting XML:

<Customers>
    <Customer_x0020_Name>Sean</Customer_x0020_Name>
  </Customers>
  <Customers>
    <Customer_x0020_Name>John</Customer_x0020_Name>
  </Customers>
  <Customers>
    <Customer_x0020_Name>Sarah</Customer_x0020_Name>
  </Customers>
  <Customers>
    <Customer_x0020_Name>Mark</Customer_x0020_Name>
  </Customers>
  <Customers>
    <Customer_x0020_Name>Norman</Customer_x0020_Name>
  </Customers>
解决方案

The name of your column contains a space. XML element names cannot contain a space. A space is used in XML to separate element names from attribute names, for instance:

<ElementName Attribute1="value" />

The DataTable.WriteXml method tries to write out the XML file in a consistent way so that another DataTable object can later be used to load the XML and get as close to an exact copy of the original as possible. Therefore, it replaces illegal characters with their hex-values so that the illegal characters are not lost in translation.

So, if you want to write it to XML differently, you need to either:

  • Change the name of the column in the DataTable so that it does not contain a space
  • Manually output the XML yourself using XDocument, XmlDocument, XmlWriter, or XmlSerializer and format the output however you desire
  • Output the XML as you do now, but then run an XSLT script on it to fix the formatting

这篇关于Datatable.WriteXml - 如何美元的元素名称p $ pserve空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 08:24