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

nyw*_*ere 9 .net c# xml linq ttml

我有以下XML:

<?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>
Run Code Online (Sandbox Code Playgroud)

当我加载它XDocument.Load()然后保存它XDocument.Save()没有任何更改,我有的新XML文件如下:

<?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>
Run Code Online (Sandbox Code Playgroud)

是否有一种优雅的方式来加载和保存这种XML而不改变任何东西?

谢谢!

Cyb*_*axs 5

正如Pascal所说,问题来自 xmlns="w3.org/ns/ttml"xmlns:tt="w3.org/ns/ttml".我认为XDocument.Save生成这个xml是因为默认的xml命名空间与另一个命名空间重复.(命名空间可能更多地通过valeu而不是通过密钥来识别?)

第一个选项是删除输入文件中的副本.使用这个新版本,您将不会遇到任何问题.

<?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>
Run Code Online (Sandbox Code Playgroud)

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

doc.Root.Attributes(XName.Get("tt", @"http://www.w3.org/2000/xmlns/")).Remove();
Run Code Online (Sandbox Code Playgroud)