XDocument.Save () adds an unwanted namespace for each XElement
I have the following 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> When I load it with XDocument.Load() and then save it with XDocument.Save() without any changes, the new XML file that I have looks like this:
<?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> Is there an elegant way to load and save this kind of XML without changing anything?
Thanks!
+7
nywhere
source share1 answer
As Pascal said, the problem comes from xmlns="w3.org/ns/ttml" and xmlns:tt="w3.org/ns/ttml" . I think XDocument.Save generate this xml, because the default xml namespace is duplicated with another namespace. (Namespaces are probably more identified by valeu than by key?)
The first option is to remove the duplicate in the source file. Using this new version, you will not have any problems.
<?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> The second option is to delete the duplicate namespace somewhere before saving
doc.Root.Attributes(XName.Get("tt", @"http://www.w3.org/2000/xmlns/")).Remove(); +4
Cybermaxs
source share