how to add(or ignore) XML namespace when using XElement.Load

10,637

Solution 1

How about:

XElement withoutNamespace = XElement.Load(new StringReader(XMLDetails));
XElement withNamespace = new XElement(ns + withoutNamespace.Name.LocalName,
                                      withoutNamespace.Nodes());

As a better alternative - why don't you either include the namespace when you're building up the XML, or even better, create an XElement instead of manually generating an XML string which you then read. Manually creating XML is very rarely a good idea. Aside from anything else, you're assuming that ValuePassedThrough has already been escaped, or doesn't need escaping etc. That may be valid - but it's at least a cause for concern.

Solution 2

Like this

XElement XMLDetails = new XElement(ns + "OpeningTAG", new XElement(ns + "MyNewTag", new XAttribute("Code", 14), new XAttribute("Value", 123)));

XDocument RequestDoc = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement(ns + "HeaderTag",
        new XAttribute("xmlns", ns),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi + "schemaLocation", "http://www.addressofschema.xsd"),
        new XAttribute("Version", "1"),
            XMLDetails));
Share:
10,637
Richard Reddy
Author by

Richard Reddy

I'm a Cork based front-end web developer focused primarily on C#, Angular, TypeScript and Sql Server. I've worked with many technologies over the years and have vast experience integrating third party solutions from payment gateways (Realex/PayPal/WorldPay/more), forums (yet another .net forum, phpBB), inline chat solution (LivePerson, WebEx) and much much more. If you're interested in knowing more, feel free to get in touch. My personal site is: www.richardreddy.ie

Updated on June 26, 2022

Comments

  • Richard Reddy
    Richard Reddy almost 2 years

    I am creating XML using Linq to XML and C#. It all works great except when I need to manually add in a row in the XML. This row is only added if I have a value to pass through to it, otherwise I just ignore the entire tag.

    I use XElement.Load to load in the string of text that I store in a string but when I attach it to the XML it always puts in xmlns="" at the end of my tag.

    Is there a way I can tell XElement.Load to use the existing namespace or to ignore it when putting in the string to the XML?

    Ideally I just want my string to be included to the XML being created without extra tags being added.

    Below is a sample of what I currently do:

    string XMLDetails = null;
    if (ValuePassedThrough != null)
    XMLDetails = "<MyNewTag Code=\"14\" Value=\"" + ValuePassedThrough +"\"></MyNewTag>";
    

    When I build up the XML I load the above string into my XML. It is here where xmlns="" is being added to the XMLDetails value but ideally I want this ignored as it is causing issues with the recipient when they try to read this tag.

    XNamespace ns = "http://namespace-address";
        XNamespace xsi = "http://XMLSchema-instance-address";
    
    XDocument RequestDoc = new XDocument(
        new XDeclaration("1.0", "utf-8", null),
        new XElement(ns + "HeaderTag",
            new XAttribute("xmlns", ns),
    new XAttribute(XNamespace.Xmlns + "xsi", xsi),
    new XAttribute(xsi + "schemaLocation", "http://www.addressofschema.xsd"),
            new XAttribute("Version", "1"),
                new XElement(ns + "OpeningTAG",
    

    ...My XML Code ...

    XElement.Load(new StringReader(XMLDetails))
    

    ...End of XML Code ...

    As mentioned above. My code works, it successfully outputs XML for me. Its just the MyNewTag tag I load in using XElement.Load gets xmlns="" added to the end of it which is causing me an issue.

    Any ideas how I can work around this? Thanks for your help.

    Regards, Rich