views:

1511

answers:

2

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

+5  A: 

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.

Jon Skeet
Hi Jon, thanks for your help. Your answer makes sense but when I try to use your suggestion VS tells me that System.Xml.Linq.XElement does not contain a definition for 'LocalName'. I agree with your point on manually building up the XML as a string but if I need to apply logic how can I do this in the middle of creating the XML when building it with Linq? Thanks again.
Richard Reddy
Whoops, edited to get the local name of the XName. You can build up the XML in the same way you do now - just make XMLDetails an XElement instead of a string. Unfortunately it's not entirely clear how you're using XMLDetails later on, but this should work fine. If not, please give more details - or preferably a short but complete program.
Jon Skeet
+1  A: 

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));
dmportella