简体   繁体   English

如何将命名空间添加到XML元素

[英]How to add namespace to XML elements

How can I create XML element as below with C# by adding namespace information 如何通过添加名称空间信息使用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">

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. xmlns不是标准属性,它是特殊的名称空间声明。 You should not add them directly, just add elements/attributes using a namespace and the xmlns declarations are added automatically - eg: 您不应该直接添加它们,而只需使用名称空间添加元素/属性,并且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");

adds the xsi:schemaLocation attribute and at the same time creates a xsmln:xsi="http://www.w3.org/2001/XMLSchema-instance" declaration. 添加xsi:schemaLocation属性,并同时创建xsmln:xsi="http://www.w3.org/2001/XMLSchema-instance"声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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