using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
//
namespace CNEE.IF.VCY
{
/// <summary>
/// buidUpdateFile 的摘要说明。
/// </summary>
public class buidUpdateFile : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox serverBox;
private System.Windows.Forms.TextBox descriptionBox;
private System.Windows.Forms.TextBox nameSpaceBox;
private System.Windows.Forms.TextBox mainAppBox;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox LocationBox;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox VersionBox;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public buidUpdateFile()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.serverBox = new System.Windows.Forms.TextBox();
this.descriptionBox = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.nameSpaceBox = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.mainAppBox = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.LocationBox = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.VersionBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 384);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 24);
this.button1.TabIndex = 0;
this.button1.Text = "生成程序清单";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(520, 280);
this.listBox1.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(344, 384);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.TabIndex = 0;
this.button2.Text = "写入文本文件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(440, 384);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 24);
this.button3.TabIndex = 0;
this.button3.Text = "写入xml文件";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// serverBox
//
this.serverBox.Location = new System.Drawing.Point(256, 296);
this.serverBox.Name = "serverBox";
this.serverBox.Size = new System.Drawing.Size(248, 21);
this.serverBox.TabIndex = 3;
this.serverBox.Text = "http://127.0.0.1/update/";
//
// descriptionBox
//
this.descriptionBox.Location = new System.Drawing.Point(48, 296);
this.descriptionBox.Name = "descriptionBox";
this.descriptionBox.Size = new System.Drawing.Size(120, 21);
this.descriptionBox.TabIndex = 4;
this.descriptionBox.Text = "Application autoUpdate";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 304);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 23);
this.label1.TabIndex = 5;
this.label1.Text = "描述";
//
// label2
//
this.label2.Location = new System.Drawing.Point(176, 296);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 5;
this.label2.Text = "服务器地址";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 328);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 23);
this.label3.TabIndex = 5;
this.label3.Text = "程序集名";
//
// nameSpaceBox
//
this.nameSpaceBox.Location = new System.Drawing.Point(80, 328);
this.nameSpaceBox.Name = "nameSpaceBox";
this.nameSpaceBox.Size = new System.Drawing.Size(120, 21);
this.nameSpaceBox.TabIndex = 4;
this.nameSpaceBox.Text = "update";
//
// label4
//
this.label4.Location = new System.Drawing.Point(208, 328);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(56, 23);
this.label4.TabIndex = 6;
this.label4.Text = "主程序名";
this.label4.Click += new System.EventHandler(this.label4_Click);
//
// mainAppBox
//
this.mainAppBox.Location = new System.Drawing.Point(272, 328);
this.mainAppBox.Name = "mainAppBox";
this.mainAppBox.Size = new System.Drawing.Size(88, 21);
this.mainAppBox.TabIndex = 3;
this.mainAppBox.Text = "update.exe";
//
// label5
//
this.label5.Location = new System.Drawing.Point(368, 328);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(56, 23);
this.label5.TabIndex = 6;
this.label5.Text = "Location";
//
// LocationBox
//
this.LocationBox.Enabled = false;
this.LocationBox.Location = new System.Drawing.Point(440, 328);
this.LocationBox.Name = "LocationBox";
this.LocationBox.Size = new System.Drawing.Size(64, 21);
this.LocationBox.TabIndex = 3;
this.LocationBox.Text = ".";
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 368);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(48, 23);
this.label6.TabIndex = 7;
this.label6.Text = "Version";
//
// VersionBox
//
this.VersionBox.Location = new System.Drawing.Point(64, 368);
this.VersionBox.Name = "VersionBox";
this.VersionBox.Size = new System.Drawing.Size(168, 21);
this.VersionBox.TabIndex = 4;
this.VersionBox.Text = "Version";
//
// buidUpdateFile
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 413);
this.Controls.Add(this.label6);
this.Controls.Add(this.label4);
this.Controls.Add(this.label1);
this.Controls.Add(this.descriptionBox);
this.Controls.Add(this.serverBox);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label3);
this.Controls.Add(this.nameSpaceBox);
this.Controls.Add(this.mainAppBox);
this.Controls.Add(this.label5);
this.Controls.Add(this.LocationBox);
this.Controls.Add(this.VersionBox);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "buidUpdateFile";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "buidUpdateFile";
this.Load += new System.EventHandler(this.buidUpdateFile_Load);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
if(File.Exists(rootPath+"//FileList.txt"))
File.Delete(rootPath+"//FileList.txt");
if(File.Exists(rootPath+"//FileList.xml"))
File.Delete(rootPath+"//FileList.xml");
//CreateXML("FileList.xml","AutoUpdater");
SearchDirectory(rootPath);
}
private string rootPath=Application.StartupPath;
public void SearchDirectory(string Path)
{
// Search the directory
DirectoryInfo di = new DirectoryInfo(Path);
FileInfo[] f = di.GetFiles();
string s;
listBox1.BeginUpdate();
foreach(FileInfo myFile in f)
{
s=myFile.FullName.Remove(0,rootPath.Length+1);
if(s.IndexOf("Update")!=-1||s.IndexOf("Thumbs.db")!=-1)
{
//continue;
}
else
listBox1.Items.Add(s);
}
listBox1.EndUpdate();
// Search its sub directories
DirectoryInfo[] d = di.GetDirectories();
foreach(DirectoryInfo myDir in d)
SearchDirectory(myDir.FullName);
}
private void button2_Click(object sender, System.EventArgs e)
{
//写入文本文件
if(File.Exists(rootPath+"//FileList.txt"))
File.Delete(rootPath+"//FileList.txt");
//
StreamWriter sw=new StreamWriter(rootPath+"//FileList.txt",true,System.Text.Encoding.Default);
foreach(string st in listBox1.Items)
{
sw.WriteLine(st+" "+getFileVersion(rootPath+"//"+st)+" "+ System.IO.File.GetCreationTime(rootPath+"//"+st).ToShortDateString());
}
sw.Flush();
sw.Close();
//
//下面把基本信息写入FileList文件中
//
MessageBox.Show("ok");
}
private void button3_Click(object sender, System.EventArgs e)
{
//写入XML文件
if (System.IO.File.Exists("FileList.xml"))
File.Delete("FileList.xml");
XmlTextWriter writer = new XmlTextWriter("FileList.xml", System.Text.Encoding.GetEncoding("gb2312"));
writer.Formatting = Formatting.Indented; //缩进格式
writer.Indentation = 4;
writer.WriteStartDocument();
writer.WriteStartElement("AutoUpdater", null);
writer.WriteEndElement();
//下面循环写入文件信息
writer.Flush();
writer.Close();
xmlfun();
//下面增加 文件信息
MessageBox.Show("ok");
}
private void label4_Click(object sender, System.EventArgs e)
{
}
private void buidUpdateFile_Load(object sender, System.EventArgs e)
{
VersionBox.Text=Application.ProductVersion;
string s=Application.ExecutablePath;
mainAppBox.Text=Application.ExecutablePath.Remove(0,s.LastIndexOf("//")+1);
nameSpaceBox.Text=mainAppBox.Text.Substring(0,mainAppBox.Text.LastIndexOf("."));
}
#region 为XML文件创建一级或二级节点
private XmlDocument xmlDoc;
/// <summary>
/// 创建一个XML文件
/// </summary>
/// <param name="path">XML路径</param>
/// <param name="Node">XML根结名</param>
public void CreateXML(string path, string rootNode)
{
if (!System.IO.File.Exists(path))
{
XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.GetEncoding("gb2312"));
writer.Formatting = Formatting.Indented; //缩进格式
writer.Indentation = 4;
writer.WriteStartDocument();
if (rootNode != "")
{
writer.WriteStartElement(rootNode, null);
writer.WriteEndElement();
}
writer.Flush();
writer.Close();
}
}
public string getFileVersion(string pathFile)
{
// Get the file version for the notepad.
string version;
//try
//{
string ext = null;
if (pathFile.IndexOf('.') > 0)
{
string[] fs = pathFile.Split('.');
ext = fs[fs.Length - 1];
}
if(ext.IndexOf("exe")!=-1||ext.ToLower().IndexOf("dll")!=-1)
{
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(pathFile);
version=myFileVersionInfo.FileVersion;
}
else
//}
// catch
// {
version=getCreateDate(pathFile);
// }
// string path = @"C:/WINNT/Microsoft.NET/Framework/v1.1.4322/System.dll";
// Assembly assembly = Assembly.LoadFile(path);
// AssemblyName assemblyName = assembly.GetName();
// Version version = assemblyName.Version;
return version;
}
private string getCreateDate(string pathFile)
{
string st= System.IO.File.GetCreationTime(pathFile).ToShortDateString();
string date=string.Format(st,"yyyy.mm.dd");
return date;
}
//使用Document读取及写入XML方法
/// <summary>
/// 写入基本主要声明信息
/// </summary>
private void xmlfun()
{
XmlDocument doc = new XmlDocument();
doc.Load("FileList.xml");
XmlElement Des = doc.CreateElement("description");
Des.InnerText=descriptionBox.Text;
doc.DocumentElement.InsertAfter(Des,doc.DocumentElement.LastChild);
//
XmlElement updater=doc.CreateElement("Updater");
//
XmlNode url = (XmlNode)doc.CreateElement("Url");
url.InnerText=serverBox.Text;
updater.AppendChild(url);
XmlNode time = (XmlNode)doc.CreateElement("LastUpdateTime");
string stdate=System.IO.File.GetCreationTime(mainAppBox.Text).ToShortDateString();
string date=string.Format(stdate,"yyyy-mm-dd");
time.InnerText=date;//,DateTime.Now.Date.ToShortDateString());
updater.AppendChild(time);
doc.DocumentElement.InsertAfter(updater,doc.DocumentElement.LastChild);
//
XmlElement app=doc.CreateElement("Application");
XmlAttribute appAtr=doc.CreateAttribute("applicationId");
appAtr.InnerText=nameSpaceBox.Text;
app.Attributes.Append(appAtr);
XmlNode appNode=(XmlNode)doc.CreateElement("EntryPoint");
appNode.InnerText=mainAppBox.Text;
app.AppendChild(appNode);
XmlNode appNode2=(XmlNode)doc.CreateElement("Location");
appNode2.InnerText=LocationBox.Text;
app.AppendChild(appNode2);
XmlNode appNode3=(XmlNode)doc.CreateElement("Version");
appNode3.InnerText=VersionBox.Text;
app.AppendChild(appNode3);
doc.DocumentElement.InsertAfter(app,doc.DocumentElement.LastChild);
//
XmlElement file=doc.CreateElement("Files");
foreach(string s in listBox1.Items)
{
//string ver="";
XmlElement fileNode=doc.CreateElement("File");
//fileNode.InnerText="Ver="+""+" Name="+s;
fileNode.SetAttribute("Ver",getFileVersion(rootPath+"//"+s));
fileNode.SetAttribute("Name",s);
file.AppendChild(fileNode);
//file.AppendChild(fileNode);
}
doc.DocumentElement.InsertAfter(file,doc.DocumentElement.LastChild);
doc.Save("UpdateList.xml"); //必须要存为不同的文件
}
#endregion
#region 参考
// //·使用XmlTextWriter写入XML方法
//
// private void xmlwriter()
// {
// XmlTextWriter writer= new XmlTextWriter("phone4.xml",null);
// writer.Formatting = Formatting.Indented; //缩进格式
// writer.Indentation =4;
//
// writer.WriteStartDocument();
//
// writer.WriteStartElement("Person");
//
// writer.WriteStartAttribute("ID",null);
// writer.WriteString("004");
// writer.WriteEndAttribute();
//
// writer.WriteStartElement("Name");
// writer.WriteString("testWriterName");
// writer.WriteEndElement();
//
// writer.WriteStartElement("Number");
// writer.WriteString("88888");
// writer.WriteEndElement();
//
// writer.WriteStartElement("City");
// writer.WriteString("testWriterCity");
// writer.WriteEndElement();
//
// writer.Flush();
// writer.Close();
//
// }
//
//
//
// //·使用XmlTextReader读取XML方法
//
// private void xmlread()
// {
// XmlTextReader reader = new XmlTextReader("phone.xml");
// while(reader.Read())
// {
// if(reader.LocalName.Equals("Name") || reader.LocalName.Equals("Number"))
// {
// //this.Label1.Text += reader.ReadString()+"/t";
// }
// }
// }
/*******************************************************************************************
* <?xml version="1.0" encoding="utf-8" ?>
<PhoneBook>
<phone id="001">
<Name>加菲尔德</Name>
<Number>5555555</Number>
<City>纽约</City>
<DateOfBirth>26/10/1978</DateOfBirth>
</phone>
<phone id="002">
<Name>迈克</Name>
<Number>6666666</Number>
<City>纽约</City>
<DateOfBirth>12/02/1978</DateOfBirth>
</phone>
</PhoneBook>
*******************************************************************************************/
/*********************************************
* 问:
string NodeStr="";
//取得XML文件的物理路径
string XmlPath="";
XmlPath=this.Page.Server.MapPath(MenuDate);
XmlDocument MyXmlDocument=new XmlDocument();
MyXmlDocument.Load(XmlPath);
XmlNode xn=MyXmlDocument.SelectSingleNode("//MenuGroup");
//读取MenuGroup结点
string XnNodeStr="";
XnNodeStr=xn.Attributes["Label"].Value;
//int NodeCount;
//NodeCount=xn.ChildNodes.Count;//得到MenuGroup下面子结点的个数
//开始从第一个节点遍历
XPathNavigator nav=((IXPathNavigable)MyXmlDocument).CreateNavigator();
XPathNodeIterator iter=nav.Select("/MenuData/MenuGroup[@Label='"+XnNodeStr+"']");
while(iter.MoveNext())
{
NodeStr=LoadNode(iter.Current);
}
*********
private string LoadNode(XPathNavigator lstNav)
{
string Result="";
XPathNodeIterator iterNode=lstNav.SelectDescendants(XPathNodeType.Element,false);
while(iterNode.MoveNext())
{
Result=Result+iterNode.Current.Name+":";
}
return Result;
}
以上是我想读出每个结点属性的程序,请问,怎么能把指定的结点下所有子结点的属性读出来呢?
附xml文件
<MenuData ImagesBaseURL="images/">
<MenuGroup Label="File">
<MenuItem Label="New" ID="i_New"/>
<MenuItem Label="Open" RightIcon="arrow_white.gif" RightIconWidth="15"/>
<MenuItem Label="Save" ID="i_Save"/>
<MenuItem Label="Save As..." ID="i_SaveAs"/>
</MenuGroup>
<MenuGroup Label="Edit">
<MenuItem Label="Cut" ID="i_Cut"/>
<MenuItem Label="Copy" ID="i_Copy"/>
<MenuItem Label="Paste" ID="i_Paste"/>
<MenuItem Label="IntelliSense" RightIcon="arrow_white.gif" RightIconWidth="15"/>
</MenuGroup>
</MenuData>
______________________________________________________________________________________________
答1:
System.Xml.XmlDataDocument MyDom=new XmlDataDocument();
MyDom.Load("sss.xml")
//访问任意接点的子节点的属性
MyDom.ChildNodes[i].Attributes[i];
______________________________________________________________________________________________
答2:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(Server.MapPath("/")+"nw/Menu.xml");//change this line
//get first element
XmlElement xmlRoot=xmlDoc.DocumentElement;
XmlNode node=xmlRoot.SelectSingleNode("//MenuGroup[@Label=/"Edit/"]");
if (node.HasChildNodes)
{
foreach(XmlNode xn in node.ChildNodes)
{
txtOut.Text+=xn.Name+"/r/n";
if (xn.Attributes.Count>0)
{
for(int i=0;i<xn.Attributes.Count;i++)
{
txtOut.Text+=xn.Attributes[i].Name+":"+xn.Attributes[i].Value+"/r/n";
}
}
}
}
______________________________________________________________________________________________
答3:
这个方法我知道,用我说的那种方法怎么写呢?
如果知道请回复,要不我就结贴给分了
**********************************************************************************************/
#endregion
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
//
namespace CNEE.IF.VCY
{
/// <summary>
/// buidUpdateFile 的摘要说明。
/// </summary>
public class buidUpdateFile : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox serverBox;
private System.Windows.Forms.TextBox descriptionBox;
private System.Windows.Forms.TextBox nameSpaceBox;
private System.Windows.Forms.TextBox mainAppBox;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox LocationBox;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox VersionBox;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public buidUpdateFile()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.serverBox = new System.Windows.Forms.TextBox();
this.descriptionBox = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.nameSpaceBox = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.mainAppBox = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.LocationBox = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.VersionBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 384);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 24);
this.button1.TabIndex = 0;
this.button1.Text = "生成程序清单";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(520, 280);
this.listBox1.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(344, 384);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.TabIndex = 0;
this.button2.Text = "写入文本文件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(440, 384);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 24);
this.button3.TabIndex = 0;
this.button3.Text = "写入xml文件";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// serverBox
//
this.serverBox.Location = new System.Drawing.Point(256, 296);
this.serverBox.Name = "serverBox";
this.serverBox.Size = new System.Drawing.Size(248, 21);
this.serverBox.TabIndex = 3;
this.serverBox.Text = "http://127.0.0.1/update/";
//
// descriptionBox
//
this.descriptionBox.Location = new System.Drawing.Point(48, 296);
this.descriptionBox.Name = "descriptionBox";
this.descriptionBox.Size = new System.Drawing.Size(120, 21);
this.descriptionBox.TabIndex = 4;
this.descriptionBox.Text = "Application autoUpdate";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 304);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 23);
this.label1.TabIndex = 5;
this.label1.Text = "描述";
//
// label2
//
this.label2.Location = new System.Drawing.Point(176, 296);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 5;
this.label2.Text = "服务器地址";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 328);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 23);
this.label3.TabIndex = 5;
this.label3.Text = "程序集名";
//
// nameSpaceBox
//
this.nameSpaceBox.Location = new System.Drawing.Point(80, 328);
this.nameSpaceBox.Name = "nameSpaceBox";
this.nameSpaceBox.Size = new System.Drawing.Size(120, 21);
this.nameSpaceBox.TabIndex = 4;
this.nameSpaceBox.Text = "update";
//
// label4
//
this.label4.Location = new System.Drawing.Point(208, 328);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(56, 23);
this.label4.TabIndex = 6;
this.label4.Text = "主程序名";
this.label4.Click += new System.EventHandler(this.label4_Click);
//
// mainAppBox
//
this.mainAppBox.Location = new System.Drawing.Point(272, 328);
this.mainAppBox.Name = "mainAppBox";
this.mainAppBox.Size = new System.Drawing.Size(88, 21);
this.mainAppBox.TabIndex = 3;
this.mainAppBox.Text = "update.exe";
//
// label5
//
this.label5.Location = new System.Drawing.Point(368, 328);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(56, 23);
this.label5.TabIndex = 6;
this.label5.Text = "Location";
//
// LocationBox
//
this.LocationBox.Enabled = false;
this.LocationBox.Location = new System.Drawing.Point(440, 328);
this.LocationBox.Name = "LocationBox";
this.LocationBox.Size = new System.Drawing.Size(64, 21);
this.LocationBox.TabIndex = 3;
this.LocationBox.Text = ".";
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 368);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(48, 23);
this.label6.TabIndex = 7;
this.label6.Text = "Version";
//
// VersionBox
//
this.VersionBox.Location = new System.Drawing.Point(64, 368);
this.VersionBox.Name = "VersionBox";
this.VersionBox.Size = new System.Drawing.Size(168, 21);
this.VersionBox.TabIndex = 4;
this.VersionBox.Text = "Version";
//
// buidUpdateFile
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 413);
this.Controls.Add(this.label6);
this.Controls.Add(this.label4);
this.Controls.Add(this.label1);
this.Controls.Add(this.descriptionBox);
this.Controls.Add(this.serverBox);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label3);
this.Controls.Add(this.nameSpaceBox);
this.Controls.Add(this.mainAppBox);
this.Controls.Add(this.label5);
this.Controls.Add(this.LocationBox);
this.Controls.Add(this.VersionBox);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "buidUpdateFile";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "buidUpdateFile";
this.Load += new System.EventHandler(this.buidUpdateFile_Load);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
if(File.Exists(rootPath+"//FileList.txt"))
File.Delete(rootPath+"//FileList.txt");
if(File.Exists(rootPath+"//FileList.xml"))
File.Delete(rootPath+"//FileList.xml");
//CreateXML("FileList.xml","AutoUpdater");
SearchDirectory(rootPath);
}
private string rootPath=Application.StartupPath;
public void SearchDirectory(string Path)
{
// Search the directory
DirectoryInfo di = new DirectoryInfo(Path);
FileInfo[] f = di.GetFiles();
string s;
listBox1.BeginUpdate();
foreach(FileInfo myFile in f)
{
s=myFile.FullName.Remove(0,rootPath.Length+1);
if(s.IndexOf("Update")!=-1||s.IndexOf("Thumbs.db")!=-1)
{
//continue;
}
else
listBox1.Items.Add(s);
}
listBox1.EndUpdate();
// Search its sub directories
DirectoryInfo[] d = di.GetDirectories();
foreach(DirectoryInfo myDir in d)
SearchDirectory(myDir.FullName);
}
private void button2_Click(object sender, System.EventArgs e)
{
//写入文本文件
if(File.Exists(rootPath+"//FileList.txt"))
File.Delete(rootPath+"//FileList.txt");
//
StreamWriter sw=new StreamWriter(rootPath+"//FileList.txt",true,System.Text.Encoding.Default);
foreach(string st in listBox1.Items)
{
sw.WriteLine(st+" "+getFileVersion(rootPath+"//"+st)+" "+ System.IO.File.GetCreationTime(rootPath+"//"+st).ToShortDateString());
}
sw.Flush();
sw.Close();
//
//下面把基本信息写入FileList文件中
//
MessageBox.Show("ok");
}
private void button3_Click(object sender, System.EventArgs e)
{
//写入XML文件
if (System.IO.File.Exists("FileList.xml"))
File.Delete("FileList.xml");
XmlTextWriter writer = new XmlTextWriter("FileList.xml", System.Text.Encoding.GetEncoding("gb2312"));
writer.Formatting = Formatting.Indented; //缩进格式
writer.Indentation = 4;
writer.WriteStartDocument();
writer.WriteStartElement("AutoUpdater", null);
writer.WriteEndElement();
//下面循环写入文件信息
writer.Flush();
writer.Close();
xmlfun();
//下面增加 文件信息
MessageBox.Show("ok");
}
private void label4_Click(object sender, System.EventArgs e)
{
}
private void buidUpdateFile_Load(object sender, System.EventArgs e)
{
VersionBox.Text=Application.ProductVersion;
string s=Application.ExecutablePath;
mainAppBox.Text=Application.ExecutablePath.Remove(0,s.LastIndexOf("//")+1);
nameSpaceBox.Text=mainAppBox.Text.Substring(0,mainAppBox.Text.LastIndexOf("."));
}
#region 为XML文件创建一级或二级节点
private XmlDocument xmlDoc;
/// <summary>
/// 创建一个XML文件
/// </summary>
/// <param name="path">XML路径</param>
/// <param name="Node">XML根结名</param>
public void CreateXML(string path, string rootNode)
{
if (!System.IO.File.Exists(path))
{
XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.GetEncoding("gb2312"));
writer.Formatting = Formatting.Indented; //缩进格式
writer.Indentation = 4;
writer.WriteStartDocument();
if (rootNode != "")
{
writer.WriteStartElement(rootNode, null);
writer.WriteEndElement();
}
writer.Flush();
writer.Close();
}
}
public string getFileVersion(string pathFile)
{
// Get the file version for the notepad.
string version;
//try
//{
string ext = null;
if (pathFile.IndexOf('.') > 0)
{
string[] fs = pathFile.Split('.');
ext = fs[fs.Length - 1];
}
if(ext.IndexOf("exe")!=-1||ext.ToLower().IndexOf("dll")!=-1)
{
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(pathFile);
version=myFileVersionInfo.FileVersion;
}
else
//}
// catch
// {
version=getCreateDate(pathFile);
// }
// string path = @"C:/WINNT/Microsoft.NET/Framework/v1.1.4322/System.dll";
// Assembly assembly = Assembly.LoadFile(path);
// AssemblyName assemblyName = assembly.GetName();
// Version version = assemblyName.Version;
return version;
}
private string getCreateDate(string pathFile)
{
string st= System.IO.File.GetCreationTime(pathFile).ToShortDateString();
string date=string.Format(st,"yyyy.mm.dd");
return date;
}
//使用Document读取及写入XML方法
/// <summary>
/// 写入基本主要声明信息
/// </summary>
private void xmlfun()
{
XmlDocument doc = new XmlDocument();
doc.Load("FileList.xml");
XmlElement Des = doc.CreateElement("description");
Des.InnerText=descriptionBox.Text;
doc.DocumentElement.InsertAfter(Des,doc.DocumentElement.LastChild);
//
XmlElement updater=doc.CreateElement("Updater");
//
XmlNode url = (XmlNode)doc.CreateElement("Url");
url.InnerText=serverBox.Text;
updater.AppendChild(url);
XmlNode time = (XmlNode)doc.CreateElement("LastUpdateTime");
string stdate=System.IO.File.GetCreationTime(mainAppBox.Text).ToShortDateString();
string date=string.Format(stdate,"yyyy-mm-dd");
time.InnerText=date;//,DateTime.Now.Date.ToShortDateString());
updater.AppendChild(time);
doc.DocumentElement.InsertAfter(updater,doc.DocumentElement.LastChild);
//
XmlElement app=doc.CreateElement("Application");
XmlAttribute appAtr=doc.CreateAttribute("applicationId");
appAtr.InnerText=nameSpaceBox.Text;
app.Attributes.Append(appAtr);
XmlNode appNode=(XmlNode)doc.CreateElement("EntryPoint");
appNode.InnerText=mainAppBox.Text;
app.AppendChild(appNode);
XmlNode appNode2=(XmlNode)doc.CreateElement("Location");
appNode2.InnerText=LocationBox.Text;
app.AppendChild(appNode2);
XmlNode appNode3=(XmlNode)doc.CreateElement("Version");
appNode3.InnerText=VersionBox.Text;
app.AppendChild(appNode3);
doc.DocumentElement.InsertAfter(app,doc.DocumentElement.LastChild);
//
XmlElement file=doc.CreateElement("Files");
foreach(string s in listBox1.Items)
{
//string ver="";
XmlElement fileNode=doc.CreateElement("File");
//fileNode.InnerText="Ver="+""+" Name="+s;
fileNode.SetAttribute("Ver",getFileVersion(rootPath+"//"+s));
fileNode.SetAttribute("Name",s);
file.AppendChild(fileNode);
//file.AppendChild(fileNode);
}
doc.DocumentElement.InsertAfter(file,doc.DocumentElement.LastChild);
doc.Save("UpdateList.xml"); //必须要存为不同的文件
}
#endregion
#region 参考
// //·使用XmlTextWriter写入XML方法
//
// private void xmlwriter()
// {
// XmlTextWriter writer= new XmlTextWriter("phone4.xml",null);
// writer.Formatting = Formatting.Indented; //缩进格式
// writer.Indentation =4;
//
// writer.WriteStartDocument();
//
// writer.WriteStartElement("Person");
//
// writer.WriteStartAttribute("ID",null);
// writer.WriteString("004");
// writer.WriteEndAttribute();
//
// writer.WriteStartElement("Name");
// writer.WriteString("testWriterName");
// writer.WriteEndElement();
//
// writer.WriteStartElement("Number");
// writer.WriteString("88888");
// writer.WriteEndElement();
//
// writer.WriteStartElement("City");
// writer.WriteString("testWriterCity");
// writer.WriteEndElement();
//
// writer.Flush();
// writer.Close();
//
// }
//
//
//
// //·使用XmlTextReader读取XML方法
//
// private void xmlread()
// {
// XmlTextReader reader = new XmlTextReader("phone.xml");
// while(reader.Read())
// {
// if(reader.LocalName.Equals("Name") || reader.LocalName.Equals("Number"))
// {
// //this.Label1.Text += reader.ReadString()+"/t";
// }
// }
// }
/*******************************************************************************************
* <?xml version="1.0" encoding="utf-8" ?>
<PhoneBook>
<phone id="001">
<Name>加菲尔德</Name>
<Number>5555555</Number>
<City>纽约</City>
<DateOfBirth>26/10/1978</DateOfBirth>
</phone>
<phone id="002">
<Name>迈克</Name>
<Number>6666666</Number>
<City>纽约</City>
<DateOfBirth>12/02/1978</DateOfBirth>
</phone>
</PhoneBook>
*******************************************************************************************/
/*********************************************
* 问:
string NodeStr="";
//取得XML文件的物理路径
string XmlPath="";
XmlPath=this.Page.Server.MapPath(MenuDate);
XmlDocument MyXmlDocument=new XmlDocument();
MyXmlDocument.Load(XmlPath);
XmlNode xn=MyXmlDocument.SelectSingleNode("//MenuGroup");
//读取MenuGroup结点
string XnNodeStr="";
XnNodeStr=xn.Attributes["Label"].Value;
//int NodeCount;
//NodeCount=xn.ChildNodes.Count;//得到MenuGroup下面子结点的个数
//开始从第一个节点遍历
XPathNavigator nav=((IXPathNavigable)MyXmlDocument).CreateNavigator();
XPathNodeIterator iter=nav.Select("/MenuData/MenuGroup[@Label='"+XnNodeStr+"']");
while(iter.MoveNext())
{
NodeStr=LoadNode(iter.Current);
}
*********
private string LoadNode(XPathNavigator lstNav)
{
string Result="";
XPathNodeIterator iterNode=lstNav.SelectDescendants(XPathNodeType.Element,false);
while(iterNode.MoveNext())
{
Result=Result+iterNode.Current.Name+":";
}
return Result;
}
以上是我想读出每个结点属性的程序,请问,怎么能把指定的结点下所有子结点的属性读出来呢?
附xml文件
<MenuData ImagesBaseURL="images/">
<MenuGroup Label="File">
<MenuItem Label="New" ID="i_New"/>
<MenuItem Label="Open" RightIcon="arrow_white.gif" RightIconWidth="15"/>
<MenuItem Label="Save" ID="i_Save"/>
<MenuItem Label="Save As..." ID="i_SaveAs"/>
</MenuGroup>
<MenuGroup Label="Edit">
<MenuItem Label="Cut" ID="i_Cut"/>
<MenuItem Label="Copy" ID="i_Copy"/>
<MenuItem Label="Paste" ID="i_Paste"/>
<MenuItem Label="IntelliSense" RightIcon="arrow_white.gif" RightIconWidth="15"/>
</MenuGroup>
</MenuData>
______________________________________________________________________________________________
答1:
System.Xml.XmlDataDocument MyDom=new XmlDataDocument();
MyDom.Load("sss.xml")
//访问任意接点的子节点的属性
MyDom.ChildNodes[i].Attributes[i];
______________________________________________________________________________________________
答2:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(Server.MapPath("/")+"nw/Menu.xml");//change this line
//get first element
XmlElement xmlRoot=xmlDoc.DocumentElement;
XmlNode node=xmlRoot.SelectSingleNode("//MenuGroup[@Label=/"Edit/"]");
if (node.HasChildNodes)
{
foreach(XmlNode xn in node.ChildNodes)
{
txtOut.Text+=xn.Name+"/r/n";
if (xn.Attributes.Count>0)
{
for(int i=0;i<xn.Attributes.Count;i++)
{
txtOut.Text+=xn.Attributes[i].Name+":"+xn.Attributes[i].Value+"/r/n";
}
}
}
}
______________________________________________________________________________________________
答3:
这个方法我知道,用我说的那种方法怎么写呢?
如果知道请回复,要不我就结贴给分了
**********************************************************************************************/
#endregion
}
}