C# 从不带 xmlns="" 的字符串创建 XmlElement
C# Create XmlElement from string without xmlns=""
我正在构建一个 xml 文档,我已经在最顶部声明了命名空间。
<Root xmlns="http://www.omg.org/space/xtce" xmlns:xtce="http://www.omg.org/space/xtce" ...>
在下面的某个任意级别,我想使用从字符串创建的元素来 AppendChild。我的目标是最终得到一个包含该元素而根本没有 xmlns 的文档。
这是我得到的最接近的-
string someElementStr = "<SomeElement name="foo"><SubElement name="bar" /></SomeElement>";
XmlDocumentFragment node = doc.CreateDocumentFragment();
node.InnerXml = someElementStr;
someXmlNodeWithinDoc.AppendChild(node);
此代码导致-
<SomeElement name="foo" xmlns="">
<SubElement name="bar" />
</SomeElement>
在最终文件中。
当我不必从字符串转到 XML-
时,我使用不同的构造
XmlElement elem = doc.CreateElement("SomeElement", "http://www.omg.org/space/xtce");
elem.SetAttribute("name","foo");
someXmlNodeWithinDoc.AppendChild(elem);
这正是我想要的结果。
<SomeElement name="foo">
</SomeElement>
我想在我当前的解决方案中做一些事情
node.setNamespace("http://www.omg.org/space/xtce")
那么文档将省略 xmlns 因为它与 root 相同。
有人能告诉我使用单个命名空间构建文档的惯用方法吗?其中一些元素作为字符串存储在模型中?
这个 几乎与我的相同,除了解决方案具有仅提供子元素作为字符串的奢侈("new" 下的所有内容)。我需要整个元素。
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 解决方案,因为我不想使用 XDocument、XElement...不过还是感谢您的反馈!
// 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);
我正在构建一个 xml 文档,我已经在最顶部声明了命名空间。
<Root xmlns="http://www.omg.org/space/xtce" xmlns:xtce="http://www.omg.org/space/xtce" ...>
在下面的某个任意级别,我想使用从字符串创建的元素来 AppendChild。我的目标是最终得到一个包含该元素而根本没有 xmlns 的文档。
这是我得到的最接近的-
string someElementStr = "<SomeElement name="foo"><SubElement name="bar" /></SomeElement>";
XmlDocumentFragment node = doc.CreateDocumentFragment();
node.InnerXml = someElementStr;
someXmlNodeWithinDoc.AppendChild(node);
此代码导致-
<SomeElement name="foo" xmlns="">
<SubElement name="bar" />
</SomeElement>
在最终文件中。
当我不必从字符串转到 XML-
时,我使用不同的构造XmlElement elem = doc.CreateElement("SomeElement", "http://www.omg.org/space/xtce");
elem.SetAttribute("name","foo");
someXmlNodeWithinDoc.AppendChild(elem);
这正是我想要的结果。
<SomeElement name="foo">
</SomeElement>
我想在我当前的解决方案中做一些事情
node.setNamespace("http://www.omg.org/space/xtce")
那么文档将省略 xmlns 因为它与 root 相同。
有人能告诉我使用单个命名空间构建文档的惯用方法吗?其中一些元素作为字符串存储在模型中?
这个
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 解决方案,因为我不想使用 XDocument、XElement...不过还是感谢您的反馈!
// 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);