我实现了一个包含以下数据的XML文档
<TrafficPattern>
<WayPoint>
<Radial>001</Radial>
<Distance>0.36</Distance>
<Latitude>
<Degrees>48</Degrees>
<Minutes>31.7363644</Minutes>
</Latitude>
<Longitude>
<Degrees>11</Degrees>
<Minutes>57.53425</Minutes>
</Longitude>
</WayPoint>
<WayPoint>
<Radial>090</Radial>
<Distance>0.56</Distance>
<Latitude>
<Degrees>48</Degrees>
<Minutes>31.7363644</Minutes>
</Latitude>
<Longitude>
<Degrees>11</Degrees>
<Minutes>57.53425</Minutes>
</Longitude>
</WayPoint>
<WayPoint>
<Radial>240</Radial>
<Distance>0.56</Distance>
<Latitude>
<Degrees>48</Degrees>
<Minutes>31.7363644</Minutes></Latitude>
<Longitude>
<Degrees>11</Degrees>
<Minutes>57.53425</Minutes></Longitude>
</WayPoint>
<WayPoint>
<Radial>346</Radial>
<Distance>0.56</Distance>
<Latitude>
<Degrees>48</Degrees>
<Minutes>31.7363644</Minutes></Latitude>
<Longitude>
<Degrees>11</Degrees>
<Minutes>57.53425</Minutes></Longitude>
</WayPoint>
</TrafficPattern>
我使用XMLDocument
编写了上述XML文件。
现在我想在点击按钮时从XML文件中删除一个航点。
任何人都可以建议我使用XMLDocument
删除它的方法吗?
答案 0 :(得分:2)
我相信你想使用RemoveChild API。您还可以使用SelectSingleNode或SelectNodes API查找要删除的节点,移动到其父节点并将其删除。
答案 1 :(得分:2)
您需要使用XmlNode.RemoveChild
方法,但请注意,这只有在您将其应用于您要删除的节点的父节点时才有效,否则您将获得例外:
要删除的节点不是此节点的子节点
如removing nodes from an XmlDocument中所述。
以下RemoveNode
方法基于a simple XML example。
public sealed class TestXmlNodeRemoval
{
public static void RemoveNode()
{
var xmlDocument = new XmlDocument();
var xmlTrafficPattern = xmlDocument.CreateElement("TrafficPattern");
xmlDocument.AppendChild(xmlTrafficPattern);
xmlTrafficPattern.AppendChild(CreateWayPoint(xmlDocument,
"001", "0.36", "48", "31.7363644", "11", "57.53425"));
xmlTrafficPattern.AppendChild(CreateWayPoint(xmlDocument,
"090", "0.56", "48", "31.7363644", "11", "57.53425"));
xmlTrafficPattern.AppendChild(CreateWayPoint(xmlDocument,
"240", "0.56", "48", "31.7363644", "11", "57.53425"));
xmlTrafficPattern.AppendChild(CreateWayPoint(xmlDocument,
"346", "0.56", "48", "31.7363644", "11", "57.53425"));
Console.WriteLine(@"Original traffic pattern:");
DisplayXmlDocument(xmlDocument);
// create an arbitrary criterion to remove an element
const string radialToRemove = @"090";
Console.WriteLine(@"Remove node with radial=" + radialToRemove);
if (xmlDocument.DocumentElement != null)
{
for (var i = 0; i < xmlDocument.DocumentElement.ChildNodes.Count; ++i)
{
var radial =
xmlDocument.DocumentElement.ChildNodes[i].SelectSingleNode("Radial");
if (radial == null || (radial.InnerText != radialToRemove))
{
continue;
}
var nodeToRemove = xmlDocument.DocumentElement.ChildNodes[i];
// note that you need to remove node from the Parent
if (nodeToRemove.ParentNode != null)
{
nodeToRemove.ParentNode.RemoveChild(nodeToRemove);
}
break;
}
}
Console.WriteLine(@"New traffic pattern:");
DisplayXmlDocument(xmlDocument);
}
}
以上方法创建以下输出:
原始流量模式:
径向:001,距离:0.36,纬度:(48,31.7363644),经度:(11,57.53425)
径向:090,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)
径向:240,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)
径向:346,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)
删除radial = 090
的节点新流量模式:
径向:001,距离:0.36,纬度:(48,31.7363644),经度:(11,57.53425)
径向:240,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)
径向:346,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)
我使用的支持方法复制如下。
第一种方法创建了一个航点,因此上面的代码并没有那么杂乱。为简单起见,一切都是string
,但我可能会选择更好的参数类型
private static XmlElement CreateWayPoint(XmlDocument xmlDoc,
string radial,
string distance,
string latDegrees,
string latMinutes,
string longDegrees,
string longMinutes)
{
var xmlWayPoint = xmlDoc.CreateElement("WayPoint");
var xmlRadial = xmlDoc.CreateElement("Radial");
xmlRadial.InnerText = radial;
xmlWayPoint.AppendChild(xmlRadial);
var xmlDistance = xmlDoc.CreateElement("Distance");
xmlDistance.InnerText = distance;
xmlWayPoint.AppendChild(xmlDistance);
var xmlLatitude = xmlDoc.CreateElement("Latitude");
var xmlLatDegrees = xmlDoc.CreateElement("Degrees");
xmlLatDegrees.InnerText = latDegrees;
xmlLatitude.AppendChild(xmlLatDegrees);
var xmlLatMinutes = xmlDoc.CreateElement("Minutes");
xmlLatMinutes.InnerText = latMinutes;
xmlLatitude.AppendChild(xmlLatMinutes);
xmlWayPoint.AppendChild(xmlLatitude);
var xmlLongitude = xmlDoc.CreateElement("Longitude");
var xmlLongDegrees = xmlDoc.CreateElement("Degrees");
xmlLongDegrees.InnerText = longDegrees;
xmlLongitude.AppendChild(xmlLongDegrees);
var xmlLongMinutes = xmlDoc.CreateElement("Minutes");
xmlLongMinutes.InnerText = longMinutes;
xmlLongitude.AppendChild(xmlLongMinutes);
xmlWayPoint.AppendChild(xmlLongitude);
return xmlWayPoint;
}
此方法显示XML文档:
private static void DisplayXmlDocument(XmlNode xmlDoc)
{
var wayPoints = xmlDoc.SelectNodes("TrafficPattern/WayPoint");
if (wayPoints == null)
{
return;
}
foreach (XmlNode wayPoint in wayPoints)
{
var radial = wayPoint.SelectSingleNode("Radial");
var distance = wayPoint.SelectSingleNode("Distance");
var latitudeDegrees = wayPoint.SelectSingleNode("Latitude/Degrees");
var latitudeMinutes = wayPoint.SelectSingleNode("Latitude/Minutes");
var longitudeDegrees = wayPoint.SelectSingleNode("Longitude/Degrees");
var longitudeMinutes = wayPoint.SelectSingleNode("Longitude/Minutes");
if (radial != null &&
distance != null &&
latitudeDegrees != null &&
latitudeMinutes != null &&
longitudeDegrees != null &&
longitudeMinutes != null)
{
Console.WriteLine(string.Format("Radial:{0},
Distance:{1},
Latitude:({2}, {3}),
Longitude:({4}, {5})",
radial.InnerText,
distance.InnerText,
latitudeDegrees.InnerText,
latitudeMinutes.InnerText,
longitudeDegrees.InnerText,
longitudeMinutes.InnerText));
}
}
}
答案 2 :(得分:1)
你可以只做C#,但这在XSLT中更容易完成,.NET内置了一个非常好的XSLT processor。XSLT是一个开放标准,具体而言旨在将XML转换为另一种XML(或HTML或文本)格式。
输入XSLT如下所示,将其另存为removeelement.xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="Radial"/>
<xsl:output indent="yes" />
<xsl:template match="TrafficPattern">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="WayPoint[not(Radial = $Radial)]" />
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在输入上用C#调用它很容易。 XSLT当前包含一个参数Radial
,但是您可以将其扩展为选择要删除的节点所需的任何内容,代码主要针对自身。下面是使用输入XML(称为input.xml)调用XSLT的示例:
// Load the xslt stylesheet:
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("removeelement.xslt");
// Create the XsltArgumentList with param
XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddParam("Radial", "", "090");
// do the transformation on your input (can also use Stream or TextWriter)
using (XmlWriter w = XmlWriter.Create("output-with-removed-element.xml"))
{
xslt.Transform("input.xml", xslArg, w);
}