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 に答える