0

我有一个带有分层数据的 xml 文件,我正在将其加载到 XmlDocument 对象中,到目前为止一切都很好,我现在要做的是删除所有节点和子节点,除了具有属性的节点和子节点之外

AssetSysID="6288"

我不确定如何完成这项任务,感谢任何帮助。将数据绑定到树视图的代码:

 XmlDataSource xDS = new XmlDataSource();
            XmlDocument xmlDoc = new XmlDocument();
xmlDoc = WTLIB.WTDAL.Legacy.DataTools.AssetList_XML(rcmbAssetType.SelectedValue);
                    if (xmlDoc != null)
                    {
                        #region Grid Filters
                if (Request.QueryString["grid"] == null)
                {
                    // Grid launched from Asset Screen
                    if (Request.QueryString["AssetSysID"] == null)
                        hdnGridAssetSysID.Value = "AssetSysID=" + "6288"; // Request.QueryString["AssetSysID"];
                    LoadGridData(xmlDoc);
                }

                #endregion

                xDS.Data = xmlDoc.InnerXml;
                xDS.XPath = "/xml/AMASM";
                xDS.EnableCaching = false;

                //  bind to treeview
                rtrvAMASM.Visible = true;
                rtrvAMASM.DataSource = xDS;
                rtrvAMASM.DataBind();
            }
            else
            {
                rtrvAMASM.Visible = false;
            }  
        }
        else
        {
            xDS = null;
            xmlDoc = null;
            rtrvAMASM.Visible = false;
        }

xml:

<xml>
  <AMASM AssetSysID="6288" Asset_ID="786387126" FROMLR="0.0000" Assets="786387126,(Facilities) , ( to ) Big tower-to-,Facilities">
    <AMACI ACISysID="10201" PAssetSysID="6288" Features="Sign, On the door" />
    <AMACI ACISysID="10115" PAssetSysID="6288" Features="Roof, Roof, NE corner" />
    <WMITM ITMSysID="5267" Equipment="220033,Truck #33">
      <AMSRQI SRQSysID="40" WorkOrders="E00006," />
      <AMSRQI SRQSysID="41" WorkOrders="E00007," />
      <AMSRQI SRQSysID="402" WorkOrders="E52,JDTest1" />
      <AMSRQI SRQSysID="603" WorkOrders="E0130,Wash" />
      <AMSRQI SRQSysID="656" WorkOrders="E0183,Program Error" />
      <AMSRQI SRQSysID="712" WorkOrders="E0239,Program Error" />
      <AMSRQI SRQSysID="986" WorkOrders="E0293,Testing Single Quote" />
      <AMSRQI SRQSysID="1534" WorkOrders="E0295," />
      <AMSRQI SRQSysID="1548" WorkOrders="E0296,Testing Extra fields" />
      <AMSRQI SRQSysID="1589" WorkOrders="E0299,Testing Extra fields" />
      <AMSRQI SRQSysID="1590" WorkOrders="30-2,Testing Extra fields" />
      <AMSRQI SRQSysID="2179" WorkOrders="PW12-00011,OilChange2" />
      <AMSRQI SRQSysID="2299" WorkOrders="30-3,Testing Extra fields" />
      <AMSRQI SRQSysID="2310" WorkOrders="30-4,Testing Extra fields" />
    </WMITM>
    <AMSRQI SRQSysID="1534" WorkOrders="E0295," />
    <AMSRQI SRQSysID="1548" WorkOrders="E0296,Testing Extra fields" />
    <AMSRQI SRQSysID="656" WorkOrders="E0183,Program Error" />
    <AMSRQI SRQSysID="712" WorkOrders="E0239,Program Error" />
    <AMSRQI SRQSysID="1589" WorkOrders="E0299,Testing Extra fields" />
    <AMSRQI SRQSysID="1590" WorkOrders="30-2,Testing Extra fields" />
    <AMSRQI SRQSysID="2165" WorkOrders="A00506,Test Inser" />
    <AMSRQI SRQSysID="2166" WorkOrders="A00507," />
    <AMSRQI SRQSysID="2167" WorkOrders="A00508,Test" />
    <AMSRQI SRQSysID="2299" WorkOrders="30-3,Testing Extra fields" />
    <AMSRQI SRQSysID="2310" WorkOrders="30-4,Testing Extra fields" />
    <AMFLI FLISysID="15" Floors="First Floor,Ground Level">
      <AMRMI RMISysID="21" Rooms="101,Admin Office">
        <AMSRQI SRQSysID="2165" WorkOrders="A00506,Test Inser" />
        <AMSRQI SRQSysID="2166" WorkOrders="A00507," />
        <AMSRQI SRQSysID="2167" WorkOrders="A00508,Test" />
      </AMRMI>
      <AMRMI RMISysID="22" Rooms="102,Security Office" />
      <AMRMI RMISysID="23" Rooms="103,Men's Washroom" />
      <AMRMI RMISysID="24" Rooms="104,Women's Washroom" />
    </AMFLI>
  </AMASM>
  <AMASM AssetSysID="6715" Asset_ID="HFBuild" FROMLR="0.0000" Assets="HFBuild,(Facilities) , High Falls Building,Facilities">
    <AMACI ACISysID="10132" PAssetSysID="6715" Features="" />
    <AMACI ACISysID="10133" PAssetSysID="6715" Features="" />
    <AMACI ACISysID="10134" PAssetSysID="6715" Features="" />
  </AMASM>
  <AMASM AssetSysID="6750" Asset_ID="F2000" FROMLR="0.0000" Assets="F2000,(Facilities) , Survey,Facilities" />
</xml>
4

3 回答 3

1

使用此代码

  XDocument doc = XDocument.Load("input.xml");

doc.Root.Element("Applications").Elements("ApplicationName").Remove();

doc.Save("output.xml"); // or overwrite the input if wanted with doc.Save("input.xml");
于 2013-02-13T16:26:28.803 回答
1

在这篇文章的帮助下解决了这个问题:

 int intLoop = 0;
                    XmlNodeList Nodes = xmlDoc.GetElementsByTagName("AMASM");            
                    //  Loop through the list
                    while (Nodes.Count != 0)
                    {
                        foreach (XmlNode Node in Nodes)
                        {
                            if ( ! ( (Node.Attributes["AssetSysID"].Value) == hdnGridAssetSysID.Value) )
                            {
                                Node.ParentNode.RemoveChild(Node); //   <--This line messes with our iteration and forces us to get a new list after each remove    
                                //  Stop the loop
                                break;
                            }                   
                        }
                        //  Get a refreshed list of offending nodes                
                        Nodes = xmlDoc.GetElementsByTagName("AMASM");
                        intLoop++;
                        if (intLoop > 5000) break;   // <-reason for that code is to break the loop out of infinity
                    }
                    Nodes = null;
                    intLoop = 0;  
于 2013-02-13T17:44:59.210 回答
0

在没有看到您的 xml 的情况下,我会执行以下操作:

XDocument.Load("input.xml").Descendants().Where(x => x.Attributes().Any(z => z.Name == "AssetSysID" && z.Value == "6288") == false);
于 2013-02-13T16:32:32.867 回答