2013-04-17 17 views
7

Mam następujące XML:XDocument.Save() dodać niechciane nazw na każdym 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> 

Kiedy załadować go z XDocument.Load() następnie zapisać go z XDocument.Save() bez zmian, nowy plik XML mam to jako to:

<?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> 

Czy istnieje elegancki sposób na załadowanie i zapisanie tego rodzaju XML bez zmiany?

Dzięki!

+1

Dlaczego masz xmlns = "http://www.w3.org/ns/ttml" i xmlns: tt = "http://www.w3.org/ns/ttml"? Domyślna przestrzeń nazw (xmlns) powinna wystarczyć, nie potrzeba xmlns: tt Myślę, że – Pascal

+1

To jest dobre pytanie, po prostu muszę ponownie utworzyć plik, który jest podobny. – nywhere

Odpowiedz

4

Jak powiedział Pascal, problem pochodzi z xmlns="w3.org/ns/ttml" i xmlns:tt="w3.org/ns/ttml". Myślę, że XDocument.Save generuje ten xml, ponieważ domyślny obszar nazw XML jest powielany z innym obszarem nazw. (Przestrzenie nazw mogą być lepiej identyfikowane przez valeu niż przez klucz?)

Pierwsza opcja to usunięcie duplikatu z pliku wejściowego. Używając tej nowej wersji, nie będziesz mieć żadnych problemów.

<?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> 

Druga opcja jest usunięcie zduplikowanych nazw gdzieś przed zapisać

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