简体   繁体   中英

How to add namespace to XML elements

How can I create XML element as below with C# by adding namespace information

<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">

and this is my current code

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

so i add new attributes like this

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

but it is given an error any idea about this ?

xmlns is not a standard attribute, it is a special namespace declaration. You should not add them directly, just add elements/attributes using a namespace and the xmlns declarations are added automatically - eg:

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

adds the xsi:schemaLocation attribute and at the same time creates a xsmln:xsi="http://www.w3.org/2001/XMLSchema-instance" declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM