XElement Add function adds xmlns=“” to the XElement?

Set the namespace on the Example and SubElement elements to the same as the RootElement. It is adding the xmlns="" to clear the namespace for these elements.

Ok I now understand why it does that, thank you! But it's only useful if you merge different xml, with different namespaces, not while creating a single xml document. Furthermore an empty namespace tag isn't valid xml.

– JJoos Feb 19 '10 at 15:57 @JJoos: an empty namespace is certainly valid XML. What makes you think it isn't? Also, keep in mind a single XML document may have elements and attributes from many namespaces.

– John Saunders Mar 16 '10 at 19:36 @John Saunders you're right, thought it was illegal because of: w3. Org/TR/REC-xml-names Section 2.2 first line. Kind of confusing.Thanks.

– JJoos Mar 19 '10 at 9:07.

Here is an example that outputs xml without empty namespaces. Notice the bizarre Linq-centric syntax rootNamespace + "MyElementName", which is the secret. This is the same namespace as the whole document, thus no xmlns addition is necessary.

This is concatenating an XNamespace + a string, which is a "+" overload that works for Linq and that Linq knows how to deal with. (Without Linq it could be a compile error to concatenate a string and a non string type). Note this was executed against a C# project file which is a handy Xml file.

Output it to a console or a richtextbox control. Then take out the "rootNamespace +" and notice the difference. XDocument doc = null; using (StreamReader streamReader = new StreamReader(@"myXml.

Csproj")) { doc = XDocument. Load(streamReader, LoadOptions. None); } XNamespace rootNamespace = doc.Root.Name.

NamespaceName; // A search which finds the ItemGroup which has Reference // elements and returns the ItemGroup XElement. XElement element = doc.Descendants(). Where(p => p.Name.

LocalName == "ItemGroup" && p.Descendants().First().Name. LocalName == "Reference").First(); // Create a completly new element with sub elements. XElement referenceElement = new XElement(rootNamespace + "Reference", new XElement(rootNamespace + "SpecificVersion", bool.

FalseString), new XElement(rootNamespace + " "THIS IS A HINT PATH")); // Add the new element to the main doc, to the end of the Reference elements. Element. Add(referenceElement); // Add an attribute after the fact for effect.

ReferenceElement. SetAttributeValue("Include", "THIS IS AN INCLUDE"); rtb. Text = doc.

ToString(SaveOptions. None).

Thank you this looks like a really good solution! I'll try it next time I run in to this problem. – JJoos Apr 28 '10 at 8:32.

I solved it by replacing the elements with an regex. Foole's solution didn't work, because I didn't always now the exact namespace at that point in code. So here's my dirty hack that works: template = XDocument.

Parse(new Regex("") . Replace(template. ToString(SaveOptions.

DisableFormatting), "")).

Be careful with your regular expressions. XML is not a regular language so, in general, regular expressions shouldn't be used against XML. – John Saunders Mar 16 '10 at 19:38.

It could be that your root needs to be closed properly: to.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions