Write XML Fragment with LINQ and Prefixes
I have a document created in a constructor, and during execution I'm filling it in 开发者_StackOverflow社区with fragments generated from Custom Business Objects.
When I'm outputting the fragments, I need to include namespace fragments, but I'd like to avoid adding the namespace url to each fragment, since it's defined in the root.
Any thoughts?
_doc = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement(aw + "kml",
new XAttribute(XNamespace.Xmlns + "gx", "http://www.google.com/kml/ext/2.2"),
new XAttribute("xmlns", "http://www.opengis.net/kml/2.2"),
new XElement(aw+"Document",
That's how the doc starts, so the namespaces are there. How do I go about building an XElement to add using the gx prefix?
Use the same URI for an XNamespace
:
XNamespace gx = "http://www.google.com/kml/ext/2.2";
XElement foo = new XElement(gx + "foo");
LINQ to XML will use the appropriate prefix automatically, as I understand it.
精彩评论