c# - XElement adds an xmlns -


i'm using linq xml create new xml file. part of file existing xml file. use following code this.

var v2 = new xdocument(   new xdeclaration("1.0", "utf-16", ""),   new xcomment(string.format("converted version 1. date: {0}", datetime.now)),   new xelement(ns + "keyem",     new xattribute(xnamespace.xmlns + "xsd", xsd.namespacename),     new xattribute(xnamespace.xmlns + "xsi", xsi.namespacename),     new xattribute(xsi + "schemalocation", schemalocation.namespacename),     new xattribute("version", "2"),     new xattribute("description", description),     new xelement(ns + "layout",       new xattribute("type", type),       new xattribute("height", height),       new xattribute("width", width),       settings.root)       // xml existing file 

the problem adds xmlns = "" first element existing file.

the result is:

<?xml version="1.0" encoding="utf-16"?> <foo    xmlns:xsd="http://www.w3.org/2001/xmlschema"    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"   xsi:schemalocation="http://tempuri.org/keyemfileschema.xsd keyemfileschema.xsd"   xmlns="http://tempuri.org/keyemfileschema.xsd">   <settings xmlns="">       ...   </settings> </foo> 

the xml file i'm reading looks this, can change if needed

<?xml version="1.0" encoding="utf-16"?> <settings>   <colormaps>     <colormap color="gray"     textcolor="black"/>     <colormap color="darkgray" textcolor="white"/>     <colormap color="black"    textcolor="white"/>     <colormap color="cyan"     textcolor="black"/>   </colormaps>   <macromaps>     <macromap pattern="^@([0-9a-f]{2})\|([0-9a-f]{2})$"  replace="{esc}$1{esc}$2{mousereset}"/>     <macromap pattern="^\$([0-9a-f]{2})\|([0-9a-f]{2})$" replace="{userclick}{esc}$1{esc}$2{mousereset}"/>     <macromap pattern="^\$([0-9a-f]{2})$"                replace="{userclick}{esc}$1"/>   </macromaps>   <keydefault color="cyan"/>   <groupdefault color="darkgray"/> </settings> 

you're seeing because settings element (presumably coming document) not live in namespace. lives in default/null-uri namespace.

you need transform input document in order change it's namespace.

this simplified example take xml file , places document but, before so, changes namespace of every element in xml file of target document...

    static void processxmlfile()     {         xnamespace ns = "http://tempuri.org/keyemfileschema.xsd/";          // load xml document         xelement settings = xelement.load("data.xml");          // shift elements in settings document target namespace         foreach (xelement e in settings.descendantsandself())         {             e.name = ns + e.name.localname;         }          // write output document         var file = new xdocument(new xelement(ns + "foo",                                         settings));          console.write(file.tostring());                 } 

the result of this...

<foo xmlns="http://tempuri.org/keyemfileschema.xsd/">   <settings>     <colormaps>       <colormap color="gray" textcolor="black" />       <colormap color="darkgray" textcolor="white" />       <colormap color="black" textcolor="white" />       <colormap color="cyan" textcolor="black" />     </colormaps>     <macromaps>       <macromap pattern="^@([0-9a-f]{2})\|([0-9a-f]{2})$" replace="{esc}$1{esc}$2{mousereset}" />       <macromap pattern="^\$([0-9a-f]{2})\|([0-9a-f]{2})$" replace="{userclick}{esc}$1{esc}$2{mousereset}" />       <macromap pattern="^\$([0-9a-f]{2})$" replace="{userclick}{esc}$1" />     </macromaps>     <keydefault color="cyan" />     <groupdefault color="darkgray" />   </settings> </foo> 

as can see, settings element in same namespace foo element. quick , dirty xml transform, , doesn't respect namespaces in xml doc you're importing. might you're after, or might @ least form basis of more robust.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -