十二 252008
 

也许您会说这样写就可以啦.

webBrowser.DocumentText = xmlDoc.OuterXml;

其实这样是错的,这样不会显示XML的结构。

本来以为可以设置webBrowser.DocumentType=”text/xml”;

无奈,WebBrowser.DocumentType是只读类型的。

查了一下资料,原来是要用System.Xml.XslCompiledTransform去格式化一下

Ok, lets’ do it.

Example Code:
     string xml=xmlDoc.OuterXml;//xmlDoc是XmlDocument类型
     XslCompiledTransform xslt = new XslCompiledTransform();
     xslt.Load("xml-pretty-print.xsl");
     TextReader tr = new StringReader(xml);
     XPathDocument xpath = new XPathDocument(tr);
     TextWriter tw = new StringWriter();
     XmlWriter xwriter = XmlWriter.Create(tw);
     xslt.Transform(xpath, xwriter);
     xmlBrowser.DocumentText = tw.ToString();//xmlBrowser是一个WebBrowser控件

参考了一下文章:

http://www.cnblogs.com/skyfei/archive/2008/04/18/1160302.html

http://www.cnblogs.com/cstded/archive/2008/01/16/1041368.html (非常详细,推荐)

http://www.biglist.com/lists/xsl-list/archives/200003/msg00769.html

其中第二篇博文提到的defualtss.xsl俺木有获取到。就用了第三篇文章的xml-pretty-print.xsl内容是一样的:)

下载:xml-pretty-print

preview

082008
 
 
       public String GetCpuInfo()
        {
            //得到cpu信息
            string CPUInfo = "";
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                CPUInfo = mo.Properties["ProcessorId"].Value.ToString();

            }
            return CPUInfo;
        }

 

        public string GetHDInfo()
        {
            //获取硬盘信息
            string HDInfo = "";
            ManagementClass mc = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                HDInfo = (string)mo.Properties["Model"].Value;
            }
            return HDInfo;
        }
        public static void GetMacAddress()

        {
            //获取网卡硬件地址

            string MacAddress = "";
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                    MacAddress = mo["MacAddress"].ToString();
                mo.Dispose();
            }
            return MacAddress;
        }
072008
 

WinForms中如果进行大规模计算,或者进行后台网络通信/下载的时候,因为要等待事件的完成,所以会造成假死的现象。

可以用两种方法解决:

Firstly,使用多线程。单独开设一个线程,异步调用。这样就不占用界面的线程。从而避免了界面假死。

Secondly,使用BackgoundWorker类,其实也是使用单独的线程,但是M$把要做的东东封装成一个类了:)

详细请看MSDN关于BackgoundWorker类

深深佩服.Net Framework噶多的类。。。功能是如此强大!

 Posted by at 11:34 上午  Tagged with:
262008
 

Two methods.

Method 1、

string sql=”insert into………………”;
string sql2 = “select @@identity as ‘lastid'”;
            try
            {
                OpenConnection();
                SqlCommand=objSqlCommand = new SqlCommand(sql, objSqlConnection);
                objSqlCommand.ExecuteNonQuery();
                SqlDataAdapter=objSqlDataAdapter = new SqlDataAdapter(sql2, objSqlConnection);
                DataSet=objDataSet = new DataSet();
                objSqlDataAdapter.Fill(objDataSet, “identity”);
                String num = objDataSet.Tables[“identity”].Rows[0][“lastid”].ToString();
                CloseConnection();
                return int.Parse(num);
            }
            catch (Exception ex)
            {
                throw new ApplicationException(“查询ID出错n” + ex.Message);
            }

Methods 2、(From CSDN)

if you are using ms sql server

sqlcommand.CommandText = “INSERT INTO sometable (filed1) VALUES (@filed1);”
+” SELECT @ID = @@identity “;
sqlcommand.Parameter.Add(“@field1″, …);

sqlcommand.Parameter.Add(“@ID”, SqlDbType.Int, 4).Direction = ParameterDirection.Output;
sqlcommand.ExecuteNonQuery();
int id = cmd.Parameters[“@ID”].Value;

 Posted by at 11:44 上午  Tagged with: