3

我有以下 xml 结构。我想删除一个完整的节点<ColumnValues>USD</ColumnValues>

<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>

为此,我使用以下代码 -

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");

但它没有删除完整的节点。输出是这样的 -

<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>

请建议,如何从 xml 中完全删除特定节点?

4

2 回答 2

14

听起来很奇怪,没有方法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");

如果您可以使用 LINQ to XML,生活会更轻松。例如,要删除ColumnValues您只需使用的所有节点:

XDocument doc = XDocument.Load(@"C:\CASInputRequest_WOELN_Var1.xml");
doc.Descendants("ColumnValues").Remove();
doc.Save(@"C:\CASInputRequest_WOELN_Var1.xml");

或者只是第一个:

XDocument doc = XDocument.Load(@"C:\CASInputRequest_WOELN_Var1.xml");
doc.Descendants("ColumnValues").First().Remove();
doc.Save(@"C:\CASInputRequest_WOELN_Var1.xml");
于 2013-07-04T05:50:53.473 回答
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>
于 2014-10-16T06:12:29.523 回答