2013-04-17 24 views
7
添加不必要的命名空間

我有下面的XML:XDocument.Save()每個的XElement

<?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文件,我是如下:

<?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而不改變任何東西?

謝謝!

+1

之前爲什麼你的xmlns = 「http://www.w3.org/ns/ttml」 和xmlns:TT = 「http://www.w3.org/ns/ttml」?默認名稱空間(xmlns)應該足夠了,不需要xmlns:tt我認爲 – Pascal

+1

這是一個很好的問題,我只需要重新創建一個類似這樣的文件。 – nywhere

回答

4

由於帕斯卡說,問題來自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> 

第二個選項是某處刪除重複的命名空間的節省

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