9

次の 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>

でロードして、何も変更せずXDocument.Load()に保存すると、新しい XML ファイルは次のようになります。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>

何も変更せずにこの種の XML を読み込んで保存するエレガントな方法はありますか?

ありがとう!

4

1 に答える 1

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>

2番目のオプションは、保存する前に重複した名前空間をどこかで削除することです

doc.Root.Attributes(XName.Get("tt", @"http://www.w3.org/2000/xmlns/")).Remove();
于 2013-04-19T05:56:38.113 に答える