2

I am building an xml document and I have declared the namespace at the very top.

<Root xmlns="http://www.omg.org/space/xtce" xmlns:xtce="http://www.omg.org/space/xtce" ...>

At some arbitrary level below I want to AppendChild with an element created from a string. My goal is to end up with a document that contains that element without the xmlns AT ALL.

This is the closest I have gotten-

string someElementStr = "<SomeElement name="foo"><SubElement name="bar" /></SomeElement>";
XmlDocumentFragment node = doc.CreateDocumentFragment();
node.InnerXml = someElementStr;
someXmlNodeWithinDoc.AppendChild(node);

This code results in-

<SomeElement name="foo" xmlns=""> <SubElement name="bar" /> </SomeElement> in the final document.

I use a different construct when I do not have to go from a string to XML-

XmlElement elem = doc.CreateElement("SomeElement", "http://www.omg.org/space/xtce");
elem.SetAttribute("name","foo");
someXmlNodeWithinDoc.AppendChild(elem);

and this yields exactly what I want.

<SomeElement name="foo"> </SomeElement>

I would like to do something line this in my current solution node.setNamespace("http://www.omg.org/space/xtce") then the document would omit xmlns because it is same as root.

Can someone tell me the idiomatic way to build a document with a single namespace use within, where some elements are stored in the model as a string?

This issue is almost identical to mine except the solution has the luxury of only providing the sub element as a string (everything under "new"). I need the entire element.

kennyzx
  • 12,845
  • 6
  • 39
  • 83
Philip Brack
  • 1,340
  • 14
  • 26
  • You can load the XML string to a XmlDocument then get the child nodes and add it to the existing XML document. – Karthik AMR Jun 27 '17 at 17:26
  • I tried adding my element from a document created with LoadXml but it does not like cross-document AppendChild. If you are suggesting that I have the whole document as a string that is incorrect I only have some elements. – Philip Brack Jun 27 '17 at 17:36

2 Answers2

1
string xmlRoot = "<Root xmlns=\"http://www.omg.org/space/xtce\"></Root>";
string xmlChild = "<SomeElement name=\"foo\"><SubElement name = \"bar\"/></SomeElement >";
XDocument xDoc = XDocument.Parse(xmlRoot);
XDocument xChild = XDocument.Parse(xmlChild);            
xChild.Root.Name = xDoc.Root.GetDefaultNamespace() + xChild.Root.Name.LocalName;
foreach (XElement xChild2 in xChild.Root.Nodes())
{
  xChild2.Name = xDoc.Root.GetDefaultNamespace() + xChild2.Name.LocalName;
}
xDoc.Root.Add(xChild.Root);
string xmlFinal = xDoc.ToString();
shop350
  • 198
  • 1
  • 6
  • Where you have XDocument xDoc, I have been doing alot of processing with XmlDocument doc. Can I somehow add xChild to doc? – Philip Brack Jun 27 '17 at 18:27
-1

This is the solution I ended up with. I did not use @shop350 solution because I didn't want to use XDocument,XElement... Thank you for the feedback though!

// create a fragment which I am going to build my element based on text.

XmlDocumentFragment frag = doc.CreateDocumentFragment();

// here I wrap my desired element in another element "dc" for don't care that has the namespace declaration.
string str = "";
str = "<dc xmlns=\"" + xtceNamespace + "\" ><Parameter name=\"paramA\" parameterTypeRef=\"paramAType\"><AliasSet><Alias nameSpace=\"ID\" alias=\"0001\"/></AliasSet></Parameter></dc>";

// set the xml for the fragment (same namespace as doc)
frag.InnerXml = str;

// let someXmlNodeWithinDoc be of type XmlNode which I determined based on XPath search.
// Here I attach the child element "Parameter" to the XmlNode directly effectively dropping the element <dc>
someXmlNodeWithinDoc.AppendChild(frag.FirstChild.FirstChild);
Philip Brack
  • 1,340
  • 14
  • 26