[英]How to add xmlns attribute to the root element?
我必須像休耕一樣寫xml文件
<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Status>Enabled</Status>
</VersioningConfiguration>
請任何人幫助我像上面那樣寫。
LINQ to XML變得很簡單-您只需指定元素的名稱空間,它將自動包含xmlns="..."
。 您可以給它起一個別名,但是要難一些。 要生成顯示的確切文檔,您只需要:
XNamespace ns = "http://s3.amazonaws.com/doc/2006-03-01/";
var doc = new XDocument(
new XElement(ns + "VersioningConfiguration",
new XElement(ns + "Status", "Enabled")));
Console.WriteLine(doc);
到目前為止,LINQ to XML是我使用過的最好的XML API,尤其是在處理名稱空間時。 只是對XmlDocument
說不:)
XNamespace Name = "http://s3.amazonaws.com/doc/2006-03-01/";
XDocument doc=new XDocument();
XElement X1=new XElement(Name+"VersioningConfiguration","" );
XElement X2=new XElement(Name+"Status","Enabled");
X1.Add(X2);
doc.Add(X1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.