首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XDocument.Save()在每个XElement上添加不需要的命名空间

XDocument.Save()在每个XElement上添加不需要的命名空间
EN

Stack Overflow用户
提问于 2013-04-18 02:12:25
回答 1查看 1.6K关注 0票数 9

我有以下XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    xmlns="http://www.w3.org/ns/ttml" 
    xmlns:tt="http://www.w3.org/ns/ttml"     
    xmlns:tts="http://www.w3.org/ns/ttml#styling"
    xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xml:lang="fr-FR"
    ttp:timeBase="smpte" ttp:frameRate="24" ttp:frameRateMultiplier="999 1000" ttp:dropMode="nonDrop">
  <head>
    <styling>
      <style xml:id="normal" tts:fontFamily="sansSerif" tts:fontWeight="normal" tts:fontStyle="normal" tts:color="white" tts:fontSize="100%"/>
      <style xml:id="bold" tts:fontFamily="sansSerif" tts:fontWeight="bold" tts:fontStyle="normal" tts:color="white" tts:fontSize="100%"/>
      <style xml:id="italic" tts:fontFamily="sansSerif" tts:fontWeight="normal" tts:fontStyle="italic" tts:color="white" tts:fontSize="100%"/>
      <style xml:id="bolditalic" tts:fontFamily="sansSerif" tts:fontWeight="bold" tts:fontStyle="italic" tts:color="white" tts:fontSize="100%"/>
    </styling>

当我使用XDocument.Load()加载它,然后不做任何更改地用XDocument.Save()保存它时,我得到的新XML文件如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<tt:tt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.w3.org/ns/ttml" xmlns:tt="http://www.w3.org/ns/ttml"
       xmlns:tts="http://www.w3.org/ns/ttml#styling"
       xmlns:ttp="http://www.w3.org/ns/ttml#parameter"
       xml:lang="fr-FR" ttp:timeBase="smpte"     ttp:frameRate="24" ttp:frameRateMultiplier="999 1000" ttp:dropMode="nonDrop">
  <tt:head>
    <tt:styling>
      <tt:style xml:id="normal" tts:fontFamily="sansSerif" tts:fontWeight="normal"     tts:fontStyle="normal" tts:color="white" tts:fontSize="100%" />
      <tt:style xml:id="bold" tts:fontFamily="sansSerif" tts:fontWeight="bold"     tts:fontStyle="normal" tts:color="white" tts:fontSize="100%" />
      <tt:style xml:id="italic" tts:fontFamily="sansSerif" tts:fontWeight="normal"     tts:fontStyle="italic" tts:color="white" tts:fontSize="100%" />
      <tt:style xml:id="bolditalic" tts:fontFamily="sansSerif" tts:fontWeight="bold"     tts:fontStyle="italic" tts:color="white" tts:fontSize="100%" />
    </tt:styling>

有没有一种优雅的方法来加载和保存这种XML,而不需要做任何更改?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2013-04-19 13:56:38

正如帕斯卡所说,问题来自xmlns="w3.org/ns/ttml"xmlns:tt="w3.org/ns/ttml"。我认为XDocument.Save之所以生成这个xml,是因为默认的xml名称空间与另一个名称空间重复。(名称空间可能更多地由valeu标识,而不是key ?)

第一个选项是删除输入文件中的重复项。使用这个新版本,您将不会有任何问题。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    xmlns="http://www.w3.org/ns/ttml"    
    xmlns:tts="http://www.w3.org/ns/ttml#styling"
    xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xml:lang="fr-FR"
    ttp:timeBase="smpte" ttp:frameRate="24" ttp:frameRateMultiplier="999 1000" ttp:dropMode="nonDrop">
  <head>
    <styling>
      <style xml:id="normal" tts:fontFamily="sansSerif" tts:fontWeight="normal" tts:fontStyle="normal" tts:color="white" tts:fontSize="100%"/>
      <style xml:id="bold" tts:fontFamily="sansSerif" tts:fontWeight="bold" tts:fontStyle="normal" tts:color="white" tts:fontSize="100%"/>
      <style xml:id="italic" tts:fontFamily="sansSerif" tts:fontWeight="normal" tts:fontStyle="italic" tts:color="white" tts:fontSize="100%"/>
      <style xml:id="bolditalic" tts:fontFamily="sansSerif" tts:fontWeight="bold" tts:fontStyle="italic" tts:color="white" tts:fontSize="100%"/>
    </styling>

第二种选择是在保存之前删除重复的命名空间

代码语言:javascript
复制
doc.Root.Attributes(XName.Get("tt", @"http://www.w3.org/2000/xmlns/")).Remove();
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16067176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档