2013-04-17 11 views
7

ho il seguente codice XML:XDocument.Save() aggiungere namespace indesiderato su ogni 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> 

Quando carico con XDocument.Load() quindi salvarlo con XDocument.Save() senza alcuna modifica, il nuovo file XML che ho è come segue:

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

C'è un modo elegante per caricare e salvare questo tipo di XML senza modificare nulla?

Grazie!

+1

Perché devi xmlns = "http://www.w3.org/ns/ttml" e xmlns: TT = "http://www.w3.org/ns/ttml"? Lo spazio dei nomi predefinito (xmlns) dovrebbe essere sufficiente, non c'è bisogno di xmlns: tt Penso che – Pascal

+1

Questa è una buona domanda, ho solo bisogno di ricreare un file che è così .. – nywhere

risposta

4

Come ha detto Pascal, il problema deriva da xmlns="w3.org/ns/ttml" e xmlns:tt="w3.org/ns/ttml". Penso che XDocument.Save generi questo xml perché lo spazio dei nomi xml predefinito è duplicato con un altro spazio dei nomi. (Gli spazi dei nomi sono forse più identificati da valeu che da chiave?)

Prima opzione è rimuovere il duplicato nel file di input. Usando questa nuova versione, non avrai alcun problema.

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

Seconda opzione è quella di rimuovere lo spazio dei nomi duplicati da qualche parte prima del salvataggio

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