学习linq处理数据 遥远的青苹果

  

class FindAround
    {

        private static string _NameSpace = "http://iec.ch/TC57/2003/CIM-schema-cim10#";
        private static string _NameSpaceORG = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
        private static string _NamePathma = "http://www.naritech.cn/CIM/ext-schema#"; //国电南瑞的

        private static XNamespace rdf = _NameSpaceORG;
        private static XNamespace cim = _NameSpace;
        private static XNamespace cimNARI = _NamePathma;

 

 


        public void FindAroundDevice()
        {
            string whichDeviceID = "221001742";

               #region 定位设备

            XElement exsroot = XElement.Load(@"E:\lixinhai\实验\shuju_all.xml");

            Stack<string> AroundDevieceStack = new Stack<string>(); //遍历时候暂存的连接点
            Queue<string> HaveVisitedDevieceID = new Queue<string>();//访问过的设备编号
            string thisDevieIsYourSelf = string.Empty;

            //查找周围设备Connectvie node
            IEnumerable<XElement> connect = from ele in exsroot.Elements()//cim + "ACLineSegment"
                                            where ele.HasAttributes && ele.Attributes().FirstOrDefault().Name.LocalName.ToString().Contains("ID")
                                         && ele.Attribute(rdf + "ID").Value == whichDeviceID
                                            select ele;

            int i = 0;

            foreach (var item in connect)
            {
                i++;
                Console.WriteLine("设备信息" + item.ToString()); //输出设备
            }  
                     #endregion
            if (i == 0)
            {

                Console.WriteLine("无项目"); //输出设备
            }
                       
            
            #region //找到此设备的连接点信息()
                IEnumerable<XElement> TerminalConnectiveDevice = from ele in exsroot.Elements(cim + "Terminal") select ele;


                foreach (XElement awich in TerminalConnectiveDevice)
                {
                    bool findThisTeriminal = false;
                  
                    foreach (XElement Alefe in awich.Elements())
                    {
                        if (Alefe.Name.LocalName == "Terminal.ConductingEquipment")
                        {
                            if (Alefe.Attributes().FirstOrDefault().Value == "#" + whichDeviceID)
                            {
                                Console.WriteLine("设备的连接编号信息是", i.ToString());
                               findThisTeriminal=true;
                            }
                        }
                      
                        if (findThisTeriminal)
                        {
                            foreach (XElement SaveTheConnectivenode in awich.Elements())
                            {
                                if (SaveTheConnectivenode.Name.LocalName == "Terminal.ConnectivityNode")
                                {


                                    string DeviceWithShaapID = SaveTheConnectivenode.Attributes().FirstOrDefault().Value; //带有#号的RDF信息
                                    if (AroundDevieceStack.Contains(DeviceWithShaapID) == false) //去掉重复的连接点
                                    {
                                        AroundDevieceStack.Push(DeviceWithShaapID); //保存起来一个设备的两节点信息
                                        HaveVisitedDevieceID.Enqueue(DeviceWithShaapID); //除掉本身的与堆栈共同使用,使其保证每一次都不访问自己的设备的,指向前方,不能后退  
                                        thisDevieIsYourSelf = whichDeviceID;
                                    }
                                }


                            }
                        }


                    }
                }
                

#endregion

 


       

                Console.WriteLine("****************设备周围的相连接的的设备是*********");     //输出设备

                #region  根据连接点出站点 找设备

                string CureentConnectiveNode = string.Empty;
                string CureentDeviceID = string.Empty;
                string CureentNotNeedToFindDevice = string.Empty;
                int count = 0;
                while (AroundDevieceStack.Count > 0)
                {


                    count++;
                    Console.WriteLine(count.ToString());

                    CureentConnectiveNode = AroundDevieceStack.Pop(); //弹出要设备的一个连接点

                  //  if (HaveVisitedDevieceID>0) 
                    // bool  youxiao=true;
               //     CureentNotNeedToFindDevice = HaveVisitedDevieceID.Dequeue();//以后做大数据量的时候使用

         

                    
                    Console.WriteLine("连接点:" + CureentConnectiveNode);

                    IEnumerable<XElement> theConnectdDevice = from ele in exsroot.Elements() select ele;
                    bool find = false;
                    foreach (XElement aDevice in theConnectdDevice)
                    {
                        //过滤条件
                        //todo

                   
                
                   
                        if (aDevice.Name.LocalName != "Discrete" || aDevice.Name.LocalName != "Analog")
                        {
                            find = false;
                            foreach (XElement item in aDevice.Elements())
                            {
                                if (item.HasAttributes && item.Attributes().FirstOrDefault().Value == CureentConnectiveNode && aDevice.Elements().FirstOrDefault().Value !=( "#"+thisDevieIsYourSelf))  //不能找自己本身重复
                                {
                                    //      Console.WriteLine("连接的设备:" + aDevice.ToString());
                                    find = true;
                                   

                                }

                            }
                        }
                        if (find)
                        {

                            foreach (XElement item in aDevice.Elements())
                            {
                                if (item.Name.LocalName == "Terminal.ConductingEquipment")
                                {
                             
                                        CureentDeviceID = item.Attributes().FirstOrDefault().Value;
                                        if (CureentDeviceID != "#"+thisDevieIsYourSelf)
                                        {
                                            string strtemp = CureentDeviceID.Substring(1);
                                            //Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                                            Console.WriteLine("连接的设备:" + CureentDeviceID);

                                            IEnumerable<XElement> DeviceInFo = from ele in exsroot.Elements()
                                                                            where ele.HasAttributes && ele.Attributes().FirstOrDefault().Name.LocalName.ToString().Contains("ID")
                                                                         && ele.Attribute(rdf + "ID").Value == CureentDeviceID.Substring(1)
                                                                            select ele;

                                            foreach (XElement ADeviceSimpleInFo in DeviceInFo)
                                            {
                                                Console.WriteLine("设备名称是:" + ADeviceSimpleInFo.Elements(cim + "Naming.aliasName").FirstOrDefault().Value.ToString());
                                                Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                                            }
                                        }
                                }
                            }
                            
                        }

                    }
                }

         

        
      #endregion
         }
    }
}
View Code
原文地址:https://www.cnblogs.com/lixinhai/p/2158301.html