2012-05-04 14 views
2

私は別のファイルからXML文書にpageFooterを追加し、削除しようとしています:新しく作成したXElementで空の名前空間を取得しないようにするにはどうすればよいですか?

XNamespace rep = "http://developer.cognos.com/schemas/report/8.0/"; 

//remove pageFooter 
doc.Root.Descendants().Where(e=>e.Name == rep + "pageFooter").Remove(); 

//create and load pagefooter from file 
XElement pageFooter = XElement.Load(@"C:\pageFooter.xml"); 

と私は空の名前空間を取得しています:私は追加するには、次のすべてを試した

<pageFooter xmlns=""> 
    <contents> 
     .............. 

を新しい要素の名前空間が、何もうまくいかない:

XElement pagefooter = new XElement(rep+"pageFooter"); 
pageFooter = XElement.Load(@"C:\pageFooter.xml");   
//this has no effect 

pageFooter.Name = rep.GetName(pageFooter.Name.LocalName); 
//this moves the xmlns="" to the next descendant, <contents> 

pageFooter.Add(rep); 
//this has no effect 

pageFooter.SetAttributeValue("xmlns", rep); 
//this results an empty pageFooter 

任意のアイデア?ありがとう!!

答えて

2

あなたは2番目の努力を列挙して正しい軌道に乗っていました。 xmlns=""の次の子孫への移動は、pageFooterが正しいxmlnsを持っていたことを意味しますが、残りの子孫はそれをしませんでした。これを使用する:あなたは、ファイルの内容でpageFooterにある何かを交換するpageFooter = XElement.Load(file)を行う

foreach (var d in pagefooter.DescendantsAndSelf()) 
    d.Name = rep + d.Name.LocalName; 
+0

ありがとうございました!私を混乱させたのは、なぜ次の1人の子孫だけが空の名前空間を持っていたのですか?私はまだその部分を取得しません。説明できますか?再度、感謝します!! – MariusD

+0

名前空間が明示的に定義されていない場合、その名前空間は親の名前空間を継承します。以前は、pageFooterとその子孫のxmlnsはすべて ""でした。それから、あなたはあなたの変更を行い、pageFooterのxmlnsは変更されましたが、すべての子孫はまだ ""でしたが、これはxmlns = ""を 'contents'要素に追加することによって示されました。 –

+0

ああ、それは継承される...ありがとう! – MariusD

0

いつでも。 rep名前空間をあらかじめ設定すると、XElement.Load()には影響しません。

XMLファイルにpageFooterの名前空間が設定されていますか?

+0

私はそれを削除する前に、それがあったに違いない... – MariusD

+0

2つのファイルからxmlを表示できますか? –

関連する問題