0

I have a code to extract an XML document type using this web service method

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XAttribute attribute = new XAttribute(xsi + "type", "xsd:string");

XElement node_user_id = new XElement("user_id", attribute, user.code);

XDocument doc = new XDocument(new XElement("ranzcp_user", new XAttribute(XNamespace.Xmlns + "ns1", "urn:logon"), node_user_id));

 XmlDocument xmldoc = new XmlDocument();
 xmldoc.LoadXml(elem.ToString());

With the above code i was able to extract an xml document exactly like this :

<ranzcp_user xmlns:ns1="urn:logon">
   <user_id xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" p3:type="xsd:string">12345678</user_id>
</ranzcp_user>

But what i really need to have is this :

<ranzcp_user xmlns:ns1="urn:logon">
    <user_id  xsi:type="xsd:string">12345678</user_id>
</ranzcp_user>  

Is there any way i could get the format i need for the xml, and second do the xsi:type="xsd:string" attribute was neccessary when the xml data was parsed?

TIA!

under_score
  • 57
  • 1
  • 7
  • Why do you want to remove the namespace? Are you getting errors? Who is generating the xml? If you are getting errors with the namespace which uses a schema then the xml is not valid and needs to be fixed. Removing the namespace is a kludge. – jdweng May 23 '16 at 00:19
  • 3
    @under_score `xsi:type` without declaration of `xsi` prefix (`xmlns:xsi="..."`) results in something that doesn't conform with XML spec (hence doesn't qualify to be XML, and in turn can't be produced/processed by standard XML parser) – har07 May 23 '16 at 00:52
  • Agree with @har07. For more information, see [How to restrict the value of an XML element using xsi:type in XSD?](http://stackoverflow.com/questions/33808790/how-to-restrict-the-value-of-an-xml-element-using-xsitype-in-xsd) – kjhughes May 23 '16 at 01:09
  • Thanks for all the input, for now it's just the specification of the XML i need to returned from my web service, Just don't know how the other party will parse it once i give it to them – under_score May 23 '16 at 03:23

1 Answers1

3

You could explicitly define the namespace prefix so you use the canonical xsi rather than p3:

var doc = new XDocument(
    new XElement("ranzcp_user",
        new XAttribute(XNamespace.Xmlns + "ns1", "urn:logon"),
        new XAttribute(XNamespace.Xmlns + "xsi", xsi),
        new XElement("user_id", 12345678,
            new XAttribute(xsi + "type", "xsd:string")
            )
        )
    );

See this fiddle. This gives you:

<ranzcp_user xmlns:ns1="urn:logon" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <user_id xsi:type="xsd:string">12345678</user_id>
</ranzcp_user>

But, as has been said, removing the namespace prefix entirely would result in invalid XML - no conforming processor will let you create it or read it without errors.

Could it be that the 'required' XML has this prefix declared in one of the parent elements? If not, I'd suggest it's a mistake and you should investigate this before spending time trying to remove the attribute. I suspect you'd end up undoing all that effort when the consumer works out the XML is invalid.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • Charles Mager : This one will do now, yeah i'm dealing with an old documentation of this task maybe i just need to put in this XML template for now and see how it goes on their part thanks for the help guys! – under_score May 23 '16 at 22:52