2011年10月27日

C# Read XML file


C# 利用system.xml 中的class讀取XML的方式:


要讀取的XML內容如下:

<transaction>==>Root
<sqlcmd command="Insert into GlassEQPHistory(BCNo,LineID,NodeNo,EQName,Repdatetime,Funkey,Workid,WorkNo,Worktype,Pid,Groupflag,Eng_unit1,Eng_unit2,Overbakeflag,panel_angle ,criteria,Asmtype,Viewflag,Gapflag,Asmfinish,Sealfinish,Aufinish,RubbPass,SealPass,AUPass,LcdpPass,ChamberPass,Pair_no,NonMatchflg,RubxConfirmFlag,RubxPackFlag )values('7','RBAB0100','4','Rubx','20101218015044','Receive Data','F50CC011CL01','FA88','0','799','9','0','0','0','0','6','1','0','0','0','0','0','0','0','0','0','0','0','0','0','0')"/>
</transaction>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace Csharp_Console_ReadXML
{
    class Program
    {
        static void Main(string[] args)
        {
         //   XmlTextReader xRD= new XmlTextReader("C:\\4_Receive Data_20101218015044.xml");
             利用XMLDocument讀取;        
             XmlDocument xDL = new XmlDocument();
            xDL.Load("C:\\4_Receive Data_20101218015044.xml"); //Load XML檔

           
            讀取單一Node,利用SingleNode方法取得
             XmlNode xSingleNode = xDL.SelectSingleNode("//sqlcmd");
             XmlAttributeCollection xAT = xSingleNode.Attributes; //讀出該Node所有的Attribute
             for (int i = 0; i < xAT.Count; i++)
             {
                 if (xAT.Item(i).Name == "command")
                     Console.WriteLine(xAT.Item(i).Value); //讀出我們想要的attribute內容
             }

    '------------------這是分隔線--------------------------------------------------
若要讀取多個NodeList,使用SelectNodes方法
            XmlNodeList NodeList = xDL.SelectNodes("//sqlcmd");

            foreach (XmlNode item_File in NodeList)
            {
                Console.WriteLine(item_File.Name);
                XmlAttributeCollection xAT = item_File.Attributes;

                for (int i = 0; i < xAT.Count; i++)
                {
                    if (xAT.Item(i).Name == "command")
                        Console.WriteLine(xAT.Item(i).Value);
                }

                Console.WriteLine(item_File.InnerText);
            }

       
            Console.ReadLine();
        }
    }
}


C# Use OpenFileDialog

使用OpenFileDialog的方式
1.先建立一個實體
2.定義較常使用的屬性
Code Sample:


            OpenFileDialog ofd1 = new OpenFileDialog();
            ofd1.Title = "請選擇檔案";
            ofd1.InitialDirectory = "D:";
            ofd1.Filter = "txt files (*.log)|*.txt|All files (*.*)|*.*";

            if (ofd1.ShowDialog(this) == DialogResult.OK)
            {

............略
             }

SQLExpress連線出現18452 錯誤訊息

解決方式網路上有人分享
再次記錄,增加自己記憶力
1.進入SQL Express使用Windows驗證模式登入
2.點選已登入的Server,按下右鍵選擇屬性
3.選擇安全性,並在"伺服性驗證"將原先的
Windows驗證模式,改為SQL Server與Windows驗證模式