2016-10-17 5 views
0

ルート要素の2つの名前空間を持つXMLファイルを作成したいと思います。XML名前付きとデフォルトの名前空間エラーの両方を追加します

var testdoc= new XDocument(
     new XDeclaration("1.0", "utf-8", "yes"), 
     new XElement("Document", 
      new XAttribute("xmlns", "namespace1"), 
      new XAttribute(XNamespace.Xmlns + "xsi", "namespace2"), 
      new XElement("sampleElem", "content") 
     ) 
); 

これは、次のエラーを生成します:私のコードで、次の

the prefix for "namespace2" can not be redefined within the same code for starting a new element.

私はエラーを理解し、私はそれを得るなぜ私は(プレフィックス名が同じでないように)理解していません。誰でも正しい結果を得る正しい方法を知っていますか?

答えて

1

この行では、new XElement("Document",は既定で名前空間を持つ要素を既に作成しているためです。上書きしようとしている属性を指定しています。

この

XNamespace ns = "namespace1"; 

var testdoc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"), 
    new XElement(ns + "Document", 
     new XAttribute(XNamespace.Xmlns + "xsi", "namespace2"), 
     new XElement("sampleElem", "content") 
    ) 
); 
を実行します。