私は別のファイルから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
任意のアイデア?ありがとう!!
ありがとうございました!私を混乱させたのは、なぜ次の1人の子孫だけが空の名前空間を持っていたのですか?私はまだその部分を取得しません。説明できますか?再度、感謝します!! – MariusD
名前空間が明示的に定義されていない場合、その名前空間は親の名前空間を継承します。以前は、pageFooterとその子孫のxmlnsはすべて ""でした。それから、あなたはあなたの変更を行い、pageFooterのxmlnsは変更されましたが、すべての子孫はまだ ""でしたが、これはxmlns = ""を 'contents'要素に追加することによって示されました。 –
ああ、それは継承される...ありがとう! – MariusD