【发布时间】:2011-12-14 09:41:54
【问题描述】:
我目前面临一个问题。我在 C# 中加载一个 xml 文件并从中删除一些节点并附加一些节点。现在的问题是,当我从 xml 文件中删除时,会自动创建一些空行,所以我想删除这些行。
当我在 xml 中将一些节点附加到父节点时,我希望每个结束标记中都有新行
例如。我的 XML 文件是
<intro id="S0001">
<title>Introduction Title</title>
<para>This is a paragraph. Note that paragraphs can contain other block–level objects, such as lists, as well as directly containing text.</para>
<para>The introduction can contain all of the text objects that a section can contain, except that it cannot be divided into parts, sections and sub–sections.</para>
<para>The introduction can contain tables:</para>
</intro><part>
<no>Part A</no> Article Structure <sup>(Part Title)</sup><section1 id="S0002">`enter code here`
<no>Sect 1</no>
<title>First Section in Part 1 <sup>(Section 1 Title)</sup></title>
<shortsectionhead>Short Section Header</shortsectionhead>
<para>This is a section in the first part of the article.</para>
</section1><section1 id="S0003">
代码:
XmlNode partNnode = xmlDoc.SelectSingleNode("//part");
XmlNode introNode=xmlDoc.SelectSingleNode("//intro");
XmlDocumentFragment newNode=xmlDoc.CreateDocumentFragment();
newNode.InnerXml=partNnode.OuterXml;
introNode.ParentNode.InsertAfter(newNode,introNode);
partNnode.ParentNode.RemoveChild(partNnode);
partNnode = xmlDoc.SelectSingleNode("//part");
nodeList = xmlDoc.SelectNodes("//section1");
foreach (XmlNode refrangeNode in nodeList)
{
newNode=xmlDoc.CreateDocumentFragment();
newNode.InnerXml=refrangeNode.??OuterXml;
partNnode.AppendChild(newNode);
}
请帮帮我 提前致谢
【问题讨论】:
-
你能发布到目前为止你尝试过的代码吗?
-
XmlNode partNnode = xmlDoc.SelectSingleNode("//part"); XmlNode introNode=xmlDoc.SelectSingleNode("//intro"); XmlDocumentFragment newNode=xmlDoc.CreateDocumentFragment(); newNode.InnerXml=partNnode.OuterXml; introNode.ParentNode.InsertAfter(newNode,introNode); partNnode.ParentNode.RemoveChild(partNnode); partNnode = xmlDoc.SelectSingleNode("//part"); nodeList = xmlDoc.SelectNodes("//section1"); foreach (XmlNode refrangeNode in nodeList){newNode=xmlDoc.CreateDocumentFragment();newNode.InnerXml=refrangeNode.OuterXml;partNnode.AppendChild(newNode);}
标签: c# asp.net xml-parsing