2013-08-02 46 views
1

如何創建XML元素,如下面的C#加入名稱空間信息如何添加命名空間XML元素

<Booking bookingRefCode="ABC2123331" bookingAction="addOrUpdate" bookingComplete="true" xmlns="http://schemas.test.com/booking" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation="http://schemas.test.com/booking http://schemas.test.com/Current/xsd/booking.xsd"> 

,這是我當前的代碼

  xw.WriteAttributeString("bookingRefCode", book.jobRefNo); 
      xw.WriteAttributeString("bookingAction", "addOrUpdate"); 
      xw.WriteAttributeString("bookingComplete", "true"); 

,所以我加入如新的屬性這

xw.WriteAttributeString("xmlns", "http://schemas.test.com/booking"); 

,但它給出錯誤這方面有任何想法?

+0

剛剛編輯標記C#... –

+0

我認爲它應該是'xsi:schemaLocation',而不是'xmlns:schemaLocation' – MiMo

回答

0

xmlns不是標準屬性,它是一個特殊的命名空間聲明。你不應該直接添加,只是添加元素/屬性使用命名空間和xmlns聲明將被自動添加 - 例如爲:

xw.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "http://schemas.test.com/booking http://schemas.test.com/Current/xsd/booking.xsd"); 

增加了xsi:schemaLocation屬性,並在同一時間創建一個xsmln:xsi="http://www.w3.org/2001/XMLSchema-instance"聲明。