2013-04-17 6 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 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> 

क्या इस तरह के एक्सएमएल को कुछ भी बदले बिना लोड और सहेजने का एक शानदार तरीका है?

धन्यवाद!

+1

आप xmlns = "http://www.w3.org/ns/ttml" और xmlns है क्यों है: टीटी = "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 नेमस्पेस किसी अन्य नामस्थान के साथ डुप्लिकेट किया गया है। (नेमस्पेस शायद कुंजी से वैलेयू द्वारा अधिक पहचाने जाते हैं?)

पहला विकल्प आपकी इनपुट फ़ाइल में डुप्लिकेट को हटाना है। इस नए संस्करण का उपयोग करके, आपको कोई समस्या नहीं होगी।

<?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(); 
संबंधित मुद्दे