I'll imagine you have a whatever xml with given namespace
<category name=".NET" xmlns="urn:test" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<books>
<book>CLR via C#</book>
<book>Essential .NET</book>
</books>
</category>
And you want to append a new element somewhere in the existing structure.
In order to alter the xml, you don't need to remove the namespace, but you need to play along with the namespace.
Notice the namespace is used for both queries and appends.
// Parse or Load the document
var document = XDocument.Parse(xml);
var xmlns = document.Root.GetDefaultNamespace();
// Find a specific node
var node = document
.Descendants(xmlns + "books")
.FirstOrDefault();
if (node != null)
{
// Append an element
var content = new XElement(xmlns + "book", "Linq in action");
node.Add(content);
}
that will produce
<category name=".NET" xmlns="urn:test" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<books>
<book>CLR via C#</book>
<book>Essential .NET</book>
<book>Linq in action</book>
</books>
</category>
For completeness, see bellow for the mapping classes i made use of
[XmlRoot(ElementName = "category")]
public class Category
{
[XmlElement(ElementName = "books")]
public Books Books { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "books")]
public class Books
{
[XmlElement(ElementName = "book")]
public List<string> Book { get; set; }
}