I have a large XML document and I want to use the XmlSerializer
class to insert new elements whose content comes from a .NET class instance generated using xsd.exe.
This is a follow-up to the question How to deserialize a node in a large document using XmlSerializer, and uses the same xsd and generated classes that are described in that question.
Let's say that in my sample XML I want to exchange my Ford car for a BMW. I've tried the following code:
static string XmlContent = @"
<RootNode xmlns=""http://MyNamespace"">
<Cars>
<Car make=""Volkswagen"" />
<Car make=""Ford"" />
<Car make=""Opel"" />
</Cars>
</RootNode>";
private static void TestWriteMcve()
{
var doc = new XmlDocument();
doc.LoadXml(XmlContent);
var nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("myns", "http://MyNamespace");
var node = doc.DocumentElement.SelectSingleNode("myns:Cars/myns:Car[@make='Ford']", nsMgr);
var parent = node.ParentNode;
var carSerializer = new XmlSerializer(typeof(Car));
using (var writer = node.CreateNavigator().InsertAfter())
{
// WriteWhitespace needed to avoid error "WriteStartDocument cannot
// be called on writers created with ConformanceLevel.Fragment."
writer.WriteWhitespace("");
var newCar = new Car { make = "BMW" };
carSerializer.Serialize(writer, newCar);
}
parent.RemoveChild(node);
Console.WriteLine(parent.OuterXml);
}
The result I get is close to what I want:
<Cars xmlns="http://MyNamespace">
<Car make="Volkswagen" />
<Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" make="BMW" xmlns="" />
<Car make="Opel" />
</Cars>
except for all those unwanted xmlns:...
attributes on the element that was added. Where did they come from and how can I get rid of them?