2011-12-09 12 views
2

XmlElementを既存のドキュメントに追加していますが、余分な属性が追加されています。ここでは、コードは次のようになります。.NET XMLはなぜドキュメントに追加するXmlElementsにxlmns属性を追加しますか?私はそれを停止することはできますか?

XmlNode manifest = this.getManifestNode(); 
XmlElement manifestEntry = _content.CreateElement ("item", _contentPath); 
XmlAttribute id = _content.CreateAttribute ("id"); 
id.Value = "content" + getManifestNodes().Count; 
XmlAttribute href = _content.CreateAttribute ("href"); 
href.Value = splitPath [splitPath.Length - 1]; 
XmlAttribute mediaType = _content.CreateAttribute ("media-type"); 
mediaType.Value = "application/xhtml+xml"; 
manifestEntry.Attributes.Append (id); 
manifestEntry.Attributes.Append (href); 
manifestEntry.Attributes.Append (mediaType); 
manifest.AppendChild (manifestEntry); 

と結果のXML:

<item id="content3" href="test1.html" media-type="application/xhtml+xml" xmlns="/home/jdphenix/epubtest/test/OEBPS/content.opf" /> 

はどこから来る

xmlns="/home/jdphenix/epubtest/test/OEBPS/content.opf" 

のですか?追加するパスは、ディスク上のドキュメントの場所ですが、私は自分のコード(atleast、私が認識している)に追加していません。詳細を知る必要がある場合はお知らせください。

編集:私はFilburtの提案ごとに私のコードを修正し、これは正しい方向への一歩ですが、次のXMLを生成

XmlElement manifestEntry = _content.CreateElement ("item"); 

XmlElement manifestEntry = _content.CreateElement ("item", _contentPath); 

を変更:

<item id="content3" href="test1.html" media-type="application/xhtml+xml" xmlns="" /> 
+0

空のxmlns属性の問題はおそらく、http://stackoverflow.com/questions/135000/how-to-prevent-blank-xmlns-attributes-in-output-from-nets-xmldocumentの複製と見なされます – jdphenix

+0

これは、.NETのXmlクラスがxmlnsを適切に処理するために発生します。この「問題」は私がXML名前空間を理解していないことが原因です。 出力は空のxmlns属性エントリを生成していました。その理由は、それが への呼び出しによって行われたからです。XmlElement manifestEntry = _content.CreateElement( "item");代わりの のXmlElement manifestEntry = _content.CreateElement( "アイテム"、 "http://www.idpf.org/2007/opf")。 正解になるために、元の質問には詳細を記載しませんでした。 http://stackoverflow.com/questions/135000/how-to-prevent-blank-xmlns-attributes-in-output-from-nets-xmldocumentを参照してください。 – jdphenix

答えて

2

この名前空間を自分で追加しています(行2):

XmlElement manifestEntry = _content.CreateElement ("item", _contentPath); 

XmlDocument.CreateElement Method (String, String)を参照してください。最初のStringパラメータは、追加する要素の修飾名であり、2番目の文字列は名前空間です。

XmlElement manifestEntry = _content.CreateElement ("item"); 

を試してみて、すべてがうまくいくはず。

+0

私は正しい方向に向いています、ありがとう – jdphenix

関連する問題