XElement is automatically adding xmlns=“” to itself

不羁岁月 提交于 2019-12-04 22:27:28

You need to specify the namespace of the elements you are adding. e.g.

//sample XElement populate Element from database
StateRequestRecordGUID = new XElement(xmlns + "StateRequestRecordGUID");

and

//sample to add Elements to EmployerTPASeparationResponse
EmployerTPASeparationResponse = new XElement(xmlns + "EmployerTPASeparationResponse");

You need to specify the namespace for the XElement when you add it so that it matches that of the XDocument. You can do this as follows:

XElement employerTPASeperationResponse =
     new XElement(xmlns + "EmployerTPASeparationResponse");

You have to create a XNamespace for the root element, and then in the creation of the element, put the object namespace created, like this:

xmlDoc = new XDocument();
xmlDoc.Declaration = new XDeclaration("1.0", "utf-8", null);

XNamespace pageDefinition = @"http://xmlns.oracle.com/adfm/uimodel";

XElement root = new XElement(pageDefinition + "pageDefinition", new XAttribute("Package", "oracle.webcenter.portalapp.pages"));

xmlDoc.Add(root);

The above code produce the following xml file:

<?xml version="1.0" encoding="UTF-8"?>
<pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel" Package="oracle.webcenter.portalapp.pages"/>

When you create all of the other elements (EmployerTPASeparationResponse and StateRequestRecordGUID) you should that you include the namespace in the name element (in the same way that you did in the creation of your EmployerTPASeparationResponseCollection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!