c# - XElement is automatically adding xmlns="" to itself -


i creating new xdocument table. have validate document xsd document , keeps failing because add xmlns="" 1 of elements when shouldn't. here's parts of code pertinent.

    xnamespace xsi = "http://www.w3.org/2001/xmlschema-instance";                 xnamespace xmlns = "https://uidataexchange.org/schemas";                 xelement employertpaseparationresponse = null;                 xelement employertpaseparationresponsecollection = new xelement(xmlns + "employertpaseparationresponsecollection", new xattribute(xnamespace.xmlns + "xsi", xsi), new xattribute(xsi + "schemalocation", "https://uidataexchange.org/schemas separationresponse.xsd"));                 xdocument doc = new xdocument(                 new xdeclaration("1.0", null, "yes"), employertpaseparationresponsecollection);     //sample xelement populate element database     staterequestrecordguid = new xelement("staterequestrecordguid");                         staterequestrecordguid.setvalue(rdr["staterequestrecordguid"].tostring());      //sample add elements employertpaseparationresponse     employertpaseparationresponse = new xelement("employertpaseparationresponse");                     if (staterequestrecordguid != null)                     {                         employertpaseparationresponse.add(staterequestrecordguid);                     }      //the part add employertpaseparationresponse collection parent     employertpaseparationresponsecollection.add(employertpaseparationresponse); 

the above code produces following xml file.

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <employertpaseparationresponsecollection xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="https://uidataexchange.org/schemas separationresponse.xsd" xmlns="https://uidataexchange.org/schemas"> <employertpaseparationresponse xmlns="">     <staterequestrecordguid>94321098761987654321323456109883</staterequestrecordguid>   </employertpaseparationresponse> </employertpaseparationresponsecollection> 

notice element employertpaseparationresponse. has empty xmlns attribute. want happen write employertpaseparationresponse no attributes @ all.

you need specify namespace of elements adding. e.g.

//sample xelement populate element database staterequestrecordguid = new xelement(xmlns + "staterequestrecordguid"); 

and

//sample add elements employertpaseparationresponse employertpaseparationresponse = new xelement(xmlns + "employertpaseparationresponse"); 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -