c# - How can I prevent appendChild() from adding the xmlns="" -


here snippet of code.

filtertext = httputility.urldecode(filtertxt.value.tostring());                xmlwritersettings settings = new xmlwritersettings();                settings.indent = true;                textwriter tw = new streamwriter("d:\\filtertest.rdl");                tw.writeline(filtertext);                tw.close();                xmldocument reportrdl = new xmldocument();                reportrdl.load(reportfile);                nms = reportrdl.namespaceuri;                xmlnodelist fieldsnode = reportrdl.getelementsbytagname("dataset");                //xmlelement xoo = reportrdl.createelement("filters", nms);                //reportrdl.appendchild(xoo);                foreach (xmlnode fields in fieldsnode)                {                    // second document merge (the new filter file)                     xmldocument filterrdl = new xmldocument();                    filterrdl.load("d:\\filtertest.rdl");                     xmlnode imported = reportrdl.importnode(filterrdl.documentelement, true);                     fields.appendchild(imported);                    break;                }                //xmlnodelist filtersnode = reportrdl.getelementsbytagname("filters");                //foreach (xmlnode filters in filtersnode)                //{                //    filters.attributes.removenameditem("xmlns");                // }                reportrdl.save("d:\\newfilter.rdl"); 

here snippet of rdl file:

<report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/sqlserver/reporting/reportdesigner">   <datasources>     <datasource name="boss">       <rd:datasourceid>c6a8409e-71a4-4e96-86ad-b300a5b942c3</rd:datasourceid>       <connectionproperties>         <dataprovider>sql</dataprovider>         <connectstring>dats no no</connectstring>         <integratedsecurity>true</integratedsecurity>       </connectionproperties>     </datasource>   </datasources> 

further on down in file after <query>, need merge in <filters>

here file:

<filters>   <filter>     <filterexpression>=fields!roleid.value</filterexpression>     <operator>equal</operator>     <filtervalues>       <filtervalue>=27</filtervalue>     </filtervalues>   </filter> </filters> 

ends <filters xmlns=""> instead of <filters>

thanks

you need show samples of xml of 2 files you're trying merge.

what's happening in first file, reportrdl, dataset elements under default namespace declaration other "". therefore, every element under them not have namespace prefix , not override default namespace declaration in default namespace declared ancestor.

but you're importing filters elements not in namespace, , have no prefix. therefore, in order prevent them inheriting default namespace not apply them, need undeclare default namespace, done using xmlns="".

if above not case, please provide samples of 2 xml documents, we're not trying surgery in dark.

if above is case, question is, why not want xmlns="" in output? sounds it's correct output. but maybe don't want because want imported filters element in same namespace parent; in case, need change namespace of imported. namespace declarations follow.

or maybe want imported node in no namespace, output technically correct already, don't aesthetics of xmlns="". or maybe there's downstream xml consumer chokes on xmlns="" reason. tell more constraints can know how help.

update:

ok, know that

  • the elements in first file in reportdefinition namespace, url "http://schemas.microsoft.com/sqlserver/reporting/2009/01/reportdefinition". can tell because top-level element, <report>, has default namespace declaration on it: xmlns="http://schemas.microsoft.com.../reportdefinition". means <report> , descendants, element no namespace prefix considered in reportdefinition namespace.
  • the elements in imported file, <filters> should in same reportdefinition namespace. infer ms documentation, though it's not clear. of documentation unfortunately awful ... full of ill-formed xml even!
  • however imported <filters> element , descendants in no namespace (before import them), because have no namespace prefix , no default namespace declaration.

so when import <filters> element, appendchild() must act keep in same namespace it's in (i.e. no namespace) - otherwise, changing name. <filters> in no namespace different element <filters> in reportdefinition namespace. if <filters> appended under <dataset> as-is, inherit default namespace declaration new ancestor <report> , thereby namespace changed reportdefinition namespace.

however, do want in reportdefinition namespace. easiest way accomplish change imported file <filters> already in namespace:

<filters xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition">   <filter> 

then, appendchild() see <filters> in default namespace of new ancestors, , won't have undeclare default namespace. in fact, can remove default namespace declaration on <filters>, redundant.

however, can't guarantee remove it. 1 important thing understand xml namespaces matters what namespace each element in. doesn't matter prefix used indicate namespace, nor matter or how many times namespace prefix (or default namespace) declared: long each element ends in right namespace.

so, whether imported <filters> element ends having default namespace declaration (dnd) or not should not matter legitimate xml consumer. matters it's in reportdefinition namespace. if microsoft rdl processor chokes on dnd, means processor not adhere xml namespace standards.

i recommend reading short tutorial on xml namespaces... they're not complicated, despite reputation; they're ubiquitous when work standardized xml vocabularies; , understanding how declarations work can save lot of headache. this one's not short it's good. can't recommend w3schools 1 because confuses prefixes namespaces. :-p

wow, got long. felt necessary, though: can bail water day long, i'd rather plug leak, caused not understanding how xml namespace declarations work.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -