2010-03-09 54 views
0

我正在嘗試創建一個允許用戶輸入信息的asp.net web表單,然後通過XMLwriter將此信息發送到Web服務。XMLWriter語法問題

這是xml的一個片段,因爲它應該被輸出;

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body xmlns:ns1="http://its/foo.wsdl"> 

我嘗試通過代碼來操作它;

xml.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")  
xml.WriteAttributeString("xmlns", "ns1", "http://its/foo.wsdl") 

但我得到這個錯誤:

The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'. 

誰能告訴我什麼,我做錯了什麼?

謝謝。

回答

1
using (var writer = XmlWriter.Create(Console.Out)) 
{ 
    writer.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/"); 
    writer.WriteStartElement("soap", "Body", null); 
    writer.WriteAttributeString("xmlns", "ns1", null, "http://its/foo.wsdl"); 
    // ... add other tags 
    writer.WriteEndElement(); 
    writer.WriteEndElement(); 
} 
+0

@Darin,我不相信他需要明確地創建'xmlns:ns1' – 2010-03-09 08:59:34

+0

謝謝,使用這段代碼我得到空值的錯誤,說使用system.dbnull而不是這也會導致錯誤 – Phil 2010-03-09 09:08:47

+0

約翰,你能給我有關爲什麼我不需要顯式創建xmlns:ns1的更多信息?以及如何可以不用請生成上面的XML。 – Phil 2010-03-09 11:05:25

0

簡而言之,'xmlns'「屬性」不是真正的屬性。它們是名稱空間聲明。你不需要生成它們。必要時,它們將作爲生成內容到不同XML名稱空間的一部分生成。

+0

謝謝,但我需要做什麼? – Phil 2010-03-09 08:51:15

+0

忽略它們,做其他事情。 – 2010-03-09 08:59:00