I have below xml structure. I want delete a complete node <ColumnValues>USD</ColumnValues>
我有xml以下的结构。我想删除一个完整的节点
<TableValue>
<Columns>
<ColumnName>Currency</ColumnName>
<ColumnType>String</ColumnType>
<ColumnValues>USD</ColumnValues>
<ColumnValues>USD</ColumnValues>
</Columns>
<Columns>
<ColumnName>Exchange</ColumnName>
<ColumnType>String</ColumnType>
<ColumnValues>US</ColumnValues>
<ColumnValues>US</ColumnValues>
</Columns>
<Columns>
<ColumnName>Ticker</ColumnName>
<ColumnType>String</ColumnType>
<ColumnValues>C</ColumnValues>
<ColumnValues>AAPL</ColumnValues>
</Columns>
</TableValue>
For this I am using below code -
为此,我使用下面的代码 -
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\CASInputRequest_WOELN_Var1.xml");
XmlNodeList NodeTradeType = doc.GetElementsByTagName("ColumnValues");
NodeTradeType[0].RemoveAll();
doc.Save(@"C:\CASInputRequest_WOELN_Var1.xml");
but its not removing complete node. The output is coming like this -
但它没有删除完整的节点。输出就像这样 -
<TableValue>
<Columns>
<ColumnName>Currency</ColumnName>
<ColumnType>String</ColumnType>
<ColumnValues>
</ColumnValues>
<ColumnValues>USD</ColumnValues>
</Columns>
<Columns>
<ColumnName>Exchange</ColumnName>
<ColumnType>String</ColumnType>
<ColumnValues>US</ColumnValues>
<ColumnValues>US</ColumnValues>
</Columns>
<Columns>
<ColumnName>Ticker</ColumnName>
<ColumnType>String</ColumnType>
<ColumnValues>C</ColumnValues>
<ColumnValues>AAPL</ColumnValues>
</Columns>
</TableValue>
Please suggest, how to completely delete a particular node from xml?
请建议,如何从xml中完全删除特定节点?
2 个解决方案
#1
14
Odd as is sounds, there's no method on XmlNode
which will remove that node from the document. You need to ask the node's parent to remove the node:
听起来很奇怪,XmlNode上没有可以从文档中删除该节点的方法。您需要请求节点的父节点删除节点:
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\CASInputRequest_WOELN_Var1.xml");
XmlNodeList nodes = doc.GetElementsByTagName("ColumnValues");
XmlNode node = nodes[0];
node.ParentNode.RemoveChild(node);
doc.Save(@"C:\CASInputRequest_WOELN_Var1.xml");
If you can use LINQ to XML life will be easier though. For example, to remove all ColumnValues
nodes you'd just use:
如果你可以使用LINQ to XML生活会更容易。例如,要删除您刚刚使用的所有ColumnValues节点:
XDocument doc = XDocument.Load(@"C:\CASInputRequest_WOELN_Var1.xml");
doc.Descendants("ColumnValues").Remove();
doc.Save(@"C:\CASInputRequest_WOELN_Var1.xml");
Or just the first one:
或者只是第一个:
XDocument doc = XDocument.Load(@"C:\CASInputRequest_WOELN_Var1.xml");
doc.Descendants("ColumnValues").First().Remove();
doc.Save(@"C:\CASInputRequest_WOELN_Var1.xml");
#2
2
public void RemovePendingMessage(String message, String AdapterType, Configuration.Adapter xmlAdapterConfiguration, String traceSchedulerName, Int64 CorrelationID)
{
try
{
XmlNode docNode = null;
XmlDocument xmlDoc = new XmlDocument();
XmlDocument xdoc = new XmlDocument();
String date = String.Empty;
string strValue = string.Empty;
if (File.Exists(@"C:\Temp\UnsendMessages.xml"))
{
xmlDoc.Load(@"C:\Temp\UnsendMessages.xml");
XmlNode Unsendmessages = xmlDoc.DocumentElement;
XPathNavigator navigator = xmlDoc.CreateNavigator();
xdoc.LoadXml(message);
using (XmlReader reader = XmlReader.Create(new StringReader(message)))
{
reader.ReadToFollowing("Adapter");
reader.MoveToContent();
strValue = reader.GetAttribute("timestamp");
}
if (strValue != null)
{
docNode = xmlDoc.SelectSingleNode("//Adapter[@timestamp='" + strValue.ToString() +"' and @type='" + AdapterType.ToString() + "']");
docNode.RemoveAll();
}
xmlDoc.Save(@"C:\Temp\UnsendMessages.xml");
}
}
catch (Exception ex)
{
}
}
-------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<Unsendmessages>
<Adapter>
</Adapter>
<Adapter type="XMLAdapter" timestamp="2014-10-15 16:48:02Z">
<Resendmessage>
<WMRP_ORDERS>
<ORDERSCONTRACTNO>C0017</ORDERSCONTRACTNO>
<ORDERS_STATUSID>17</ORDERS_STATUSID>
<ORDERSDATERECEIVED>9/2/2014 12:00:00 AM</ORDERSDATERECEIVED>
<ORDERS_BT_ORDER_NO>O0017</ORDERS_BT_ORDER_NO>
<ORDERS_REGION_CODE>17</ORDERS_REGION_CODE>
<ORDERSSCHEDULEDSTARTDATE>9/2/2014 12:00:00 AM</ORDERSSCHEDULEDSTARTDATE>
<ORDERSSCHEDULEDENDDATE>9/10/2014 12:00:00 AM</ORDERSSCHEDULEDENDDATE>
</WMRP_ORDERS>
</Resendmessage>
</Adapter>
<Adapter type="XMLAdapter" timestamp="2014-10-15 16:47:32Z">
<Resendmessage>
<WMRP_ORDERS>
<ORDERSCONTRACTNO>C0016</ORDERSCONTRACTNO>
<ORDERS_STATUSID>16</ORDERS_STATUSID>
<ORDERSDATERECEIVED>9/2/2014 12:00:00 AM</ORDERSDATERECEIVED>
<ORDERS_BT_ORDER_NO>O0016</ORDERS_BT_ORDER_NO>
<ORDERS_REGION_CODE>16</ORDERS_REGION_CODE>
<ORDERSSCHEDULEDSTARTDATE>9/2/2014 12:00:00 AM</ORDERSSCHEDULEDSTARTDATE>
<ORDERSSCHEDULEDENDDATE>9/10/2014 12:00:00 AM</ORDERSSCHEDULEDENDDATE>
</WMRP_ORDERS>
</Resendmessage>
</Adapter>
<Adapter type="XMLAdapter" timestamp="2014-10-15 16:46:58Z">
<Resendmessage>
<WMRP_ORDERS>
<ORDERSCONTRACTNO>C0017</ORDERSCONTRACTNO>
<ORDERS_STATUSID>17</ORDERS_STATUSID>
<ORDERSDATERECEIVED>9/2/2014 12:00:00 AM</ORDERSDATERECEIVED>
<ORDERS_BT_ORDER_NO>O0017</ORDERS_BT_ORDER_NO>
<ORDERS_REGION_CODE>17</ORDERS_REGION_CODE>
<ORDERSSCHEDULEDSTARTDATE>9/2/2014 12:00:00 AM</ORDERSSCHEDULEDSTARTDATE>
<ORDERSSCHEDULEDENDDATE>9/10/2014 12:00:00 AM</ORDERSSCHEDULEDENDDATE>
</WMRP_ORDERS>
</Resendmessage>
</Adapter>
<Adapter type="XMLAdapter" timestamp="2014-10-15 16:46:25Z">
<Resendmessage>
<WMRP_ORDERS>
<ORDERSCONTRACTNO>C0016</ORDERSCONTRACTNO>
<ORDERS_STATUSID>16</ORDERS_STATUSID>
<ORDERSDATERECEIVED>9/2/2014 12:00:00 AM</ORDERSDATERECEIVED>
<ORDERS_BT_ORDER_NO>O0016</ORDERS_BT_ORDER_NO>
<ORDERS_REGION_CODE>16</ORDERS_REGION_CODE>
<ORDERSSCHEDULEDSTARTDATE>9/2/2014 12:00:00 AM</ORDERSSCHEDULEDSTARTDATE>
<ORDERSSCHEDULEDENDDATE>9/10/2014 12:00:00 AM</ORDERSSCHEDULEDENDDATE>
</WMRP_ORDERS>
</Resendmessage>
</Adapter>
<Adapter>
</Adapter>
</Unsendmessages>
#1
14
Odd as is sounds, there's no method on XmlNode
which will remove that node from the document. You need to ask the node's parent to remove the node:
听起来很奇怪,XmlNode上没有可以从文档中删除该节点的方法。您需要请求节点的父节点删除节点:
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\CASInputRequest_WOELN_Var1.xml");
XmlNodeList nodes = doc.GetElementsByTagName("ColumnValues");
XmlNode node = nodes[0];
node.ParentNode.RemoveChild(node);
doc.Save(@"C:\CASInputRequest_WOELN_Var1.xml");
If you can use LINQ to XML life will be easier though. For example, to remove all ColumnValues
nodes you'd just use:
如果你可以使用LINQ to XML生活会更容易。例如,要删除您刚刚使用的所有ColumnValues节点:
XDocument doc = XDocument.Load(@"C:\CASInputRequest_WOELN_Var1.xml");
doc.Descendants("ColumnValues").Remove();
doc.Save(@"C:\CASInputRequest_WOELN_Var1.xml");
Or just the first one:
或者只是第一个:
XDocument doc = XDocument.Load(@"C:\CASInputRequest_WOELN_Var1.xml");
doc.Descendants("ColumnValues").First().Remove();
doc.Save(@"C:\CASInputRequest_WOELN_Var1.xml");
#2
2
public void RemovePendingMessage(String message, String AdapterType, Configuration.Adapter xmlAdapterConfiguration, String traceSchedulerName, Int64 CorrelationID)
{
try
{
XmlNode docNode = null;
XmlDocument xmlDoc = new XmlDocument();
XmlDocument xdoc = new XmlDocument();
String date = String.Empty;
string strValue = string.Empty;
if (File.Exists(@"C:\Temp\UnsendMessages.xml"))
{
xmlDoc.Load(@"C:\Temp\UnsendMessages.xml");
XmlNode Unsendmessages = xmlDoc.DocumentElement;
XPathNavigator navigator = xmlDoc.CreateNavigator();
xdoc.LoadXml(message);
using (XmlReader reader = XmlReader.Create(new StringReader(message)))
{
reader.ReadToFollowing("Adapter");
reader.MoveToContent();
strValue = reader.GetAttribute("timestamp");
}
if (strValue != null)
{
docNode = xmlDoc.SelectSingleNode("//Adapter[@timestamp='" + strValue.ToString() +"' and @type='" + AdapterType.ToString() + "']");
docNode.RemoveAll();
}
xmlDoc.Save(@"C:\Temp\UnsendMessages.xml");
}
}
catch (Exception ex)
{
}
}
-------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<Unsendmessages>
<Adapter>
</Adapter>
<Adapter type="XMLAdapter" timestamp="2014-10-15 16:48:02Z">
<Resendmessage>
<WMRP_ORDERS>
<ORDERSCONTRACTNO>C0017</ORDERSCONTRACTNO>
<ORDERS_STATUSID>17</ORDERS_STATUSID>
<ORDERSDATERECEIVED>9/2/2014 12:00:00 AM</ORDERSDATERECEIVED>
<ORDERS_BT_ORDER_NO>O0017</ORDERS_BT_ORDER_NO>
<ORDERS_REGION_CODE>17</ORDERS_REGION_CODE>
<ORDERSSCHEDULEDSTARTDATE>9/2/2014 12:00:00 AM</ORDERSSCHEDULEDSTARTDATE>
<ORDERSSCHEDULEDENDDATE>9/10/2014 12:00:00 AM</ORDERSSCHEDULEDENDDATE>
</WMRP_ORDERS>
</Resendmessage>
</Adapter>
<Adapter type="XMLAdapter" timestamp="2014-10-15 16:47:32Z">
<Resendmessage>
<WMRP_ORDERS>
<ORDERSCONTRACTNO>C0016</ORDERSCONTRACTNO>
<ORDERS_STATUSID>16</ORDERS_STATUSID>
<ORDERSDATERECEIVED>9/2/2014 12:00:00 AM</ORDERSDATERECEIVED>
<ORDERS_BT_ORDER_NO>O0016</ORDERS_BT_ORDER_NO>
<ORDERS_REGION_CODE>16</ORDERS_REGION_CODE>
<ORDERSSCHEDULEDSTARTDATE>9/2/2014 12:00:00 AM</ORDERSSCHEDULEDSTARTDATE>
<ORDERSSCHEDULEDENDDATE>9/10/2014 12:00:00 AM</ORDERSSCHEDULEDENDDATE>
</WMRP_ORDERS>
</Resendmessage>
</Adapter>
<Adapter type="XMLAdapter" timestamp="2014-10-15 16:46:58Z">
<Resendmessage>
<WMRP_ORDERS>
<ORDERSCONTRACTNO>C0017</ORDERSCONTRACTNO>
<ORDERS_STATUSID>17</ORDERS_STATUSID>
<ORDERSDATERECEIVED>9/2/2014 12:00:00 AM</ORDERSDATERECEIVED>
<ORDERS_BT_ORDER_NO>O0017</ORDERS_BT_ORDER_NO>
<ORDERS_REGION_CODE>17</ORDERS_REGION_CODE>
<ORDERSSCHEDULEDSTARTDATE>9/2/2014 12:00:00 AM</ORDERSSCHEDULEDSTARTDATE>
<ORDERSSCHEDULEDENDDATE>9/10/2014 12:00:00 AM</ORDERSSCHEDULEDENDDATE>
</WMRP_ORDERS>
</Resendmessage>
</Adapter>
<Adapter type="XMLAdapter" timestamp="2014-10-15 16:46:25Z">
<Resendmessage>
<WMRP_ORDERS>
<ORDERSCONTRACTNO>C0016</ORDERSCONTRACTNO>
<ORDERS_STATUSID>16</ORDERS_STATUSID>
<ORDERSDATERECEIVED>9/2/2014 12:00:00 AM</ORDERSDATERECEIVED>
<ORDERS_BT_ORDER_NO>O0016</ORDERS_BT_ORDER_NO>
<ORDERS_REGION_CODE>16</ORDERS_REGION_CODE>
<ORDERSSCHEDULEDSTARTDATE>9/2/2014 12:00:00 AM</ORDERSSCHEDULEDSTARTDATE>
<ORDERSSCHEDULEDENDDATE>9/10/2014 12:00:00 AM</ORDERSSCHEDULEDENDDATE>
</WMRP_ORDERS>
</Resendmessage>
</Adapter>
<Adapter>
</Adapter>
</Unsendmessages>