Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用linq保存**EXCEL**XML文件?_Xml_Vb.net_Linq_Excel - Fatal编程技术网

如何使用linq保存**EXCEL**XML文件?

如何使用linq保存**EXCEL**XML文件?,xml,vb.net,linq,excel,Xml,Vb.net,Linq,Excel,我正在使用XElement加载和保存Excel XML文件,并使用以下blurb: Dim root As XElement = XElement.Load(inFile) 'code to change elements goes here root.Save(outFile) 问题是,Save例程正在添加名称空间标记以及谁知道是什么,因此Excel和windows不再将其识别为Excel XML文件。在我的示例中,我甚至没有操作元素。我正在加载并保存文件。我基本上

我正在使用XElement加载和保存Excel XML文件,并使用以下blurb:

    Dim root As XElement = XElement.Load(inFile)
    'code to change elements goes here 
    root.Save(outFile)
问题是,Save例程正在添加名称空间标记以及谁知道是什么,因此Excel和windows不再将其识别为Excel XML文件。在我的示例中,我甚至没有操作元素。我正在加载并保存文件。我基本上希望使用linq查找XML中的某些元素,更改这些元素,然后保存整个文件。我是不是太难了

内嵌XML

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
  <Worksheet ss:Name="Datagrid">
    <Table ss:ExpandedColumnCount="13" ss:ExpandedRowCount="11" x:FullColumns="1"
     x:FullRows="1" ss:DefaultRowHeight="15">
      <Row ss:Index="3" ss:AutoFitHeight="0">
        <Cell ecProperty="email_address">
          <Data ss:Type="String">email address</Data>
        </Cell>
      </Row>
      <Row ss:Index="4" ss:AutoFitHeight="0">
        <Cell ecProperty="synthesis_mode">
          <Data ss:Type="String">Ideal Mode</Data>
        </Cell>
      </Row>
    </Table>
  </Worksheet>
</Workbook>

电子邮件地址
理想模式
输出文件结果

<?xml version="1.0" encoding="utf-8"?>
<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
  <ss:Worksheet ss:Name="Datagrid">
    <ss:Table ss:ExpandedColumnCount="13" ss:ExpandedRowCount="11" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15">
      <ss:Row ss:Index="3" ss:AutoFitHeight="0">
        <ss:Cell ecProperty="email_address">
          <ss:Data ss:Type="String">email address</ss:Data>
        </ss:Cell>
      </ss:Row>
      <ss:Row ss:Index="4" ss:AutoFitHeight="0">
        <ss:Cell ecProperty="synthesis_mode">
          <ss:Data ss:Type="String">Ideal Mode</ss:Data>
        </ss:Cell>
      </ss:Row>
    </ss:Table>
  </ss:Worksheet>
</ss:Workbook>

电子邮件地址
理想模式
使用而不是
XElement
保存完整文档

Dim root As XDocument = XDocument.Load(inFile)
'code to change elements goes here 
root.Save(outFile)

当默认名称空间也在前缀中定义时,这似乎是一个难题…问题是没有保存。如果我手动添加,我会得到想要的结果。恐怕我没有跟上你。这很有效!我会把这个标记为正确的,但是你知道为什么吗(或者如何防止保存时添加ss:namespace?原因很简单:
XElement
不存储周围的节点,而只存储一个元素和内部的所有内容。
XDocument
存储完整的文档,包括任何XML声明、注释、处理指令等节点。但是,我没有解决此问题的方法。)名称空间前缀已找到,这似乎是由于使用前缀重新声明用作默认名称空间的名称空间,并且该名称空间仅使用前缀。但是,由于需要前缀的属性,无法删除前缀。