using System; using System.Xml;public class Sample { public static void Main() {
// Create the XmlDocument. XmlDocument doc = new XmlDocument(); doc.LoadXml("<item><name>wrench</name></item>"); // Add a price element. XmlElement newElem = doc.CreateElement("price"); newElem.InnerText = "10.95"; doc.DocumentElement.AppendChild(newElem); // Save the document to a file and auto-indent the output. XmlTextWriter writer = new XmlTextWriter("data.xml",null); writer.Formatting = Formatting.Indented; doc.Save(writer); } }
.NET Framework 开发人员指南 将 XML 文档读入 DOM 请参见 语言筛选器: 全部 语言筛选器: 多个 语言筛选器: Visual Basic 语言筛选器: C# 语言筛选器: C++ 语言筛选器: J# 语言筛选器: JScript Visual Basic(声明) Visual Basic(用法) C# C++ J# JScript XML 信息从不同的格式读入内存。读取源包括字符串、流、URL、文本读取器或 XmlReader 的派生类。Load 方法将文档置入内存中并包含可用于从每个不同的格式中获取数据的重载方法。还存在 LoadXml 方法,该方法从字符串中读取 XML。不同的 Load 方法影响在加载 XML 文档对象模型 (DOM) 时创建的节点。下表列出了一些 Load 方法的区别以及讲述这些区别的主题。课题 主题 创建空白节点 用来加载 DOM 的对象对 DOM 中生成的空白和有效空白节点有影响。有关更多信息,请参见加载 DOM 时的空白和有效空白处理。
从特定节点开始加载 XML 或加载整个 XML 文档 使用 System.Xml.XmlDocument.Load(System.Xml.XmlReader) 方法可以将数据从特定的节点加载到 DOM 中。有关更多信息,请参见从读取器中加载数据。
在 XML 加载时进行验证 加载到 DOM 中的 XML 数据可以在加载时进行验证。使用验证 XmlReader 可以完成此操作。有关在 XML 加载时进行验证的更多信息,请参见在 DOM 中验证 XML 文档。 以下示例显示使用 LoadXml 方法加载的 XML 以及之后保存到称为 data.xml 的文本文件的数据。using System; using System.IO; using System.Xml;public class Sample { public static void Main() { // Create the XmlDocument. XmlDocument doc = new XmlDocument(); doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>"); // Save the document to a file. doc.Save("data.xml"); } } 请参见 概念 XML 文档对象模型 (DOM) 要提出有关“帮助”或本产品其他功能的建议或错误报告,请转到反馈站点。
using System;
using System.Xml;public class Sample { public static void Main() {
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>"); // Add a price element.
XmlElement newElem = doc.CreateElement("price");
newElem.InnerText = "10.95";
doc.DocumentElement.AppendChild(newElem); // Save the document to a file and auto-indent the output.
XmlTextWriter writer = new XmlTextWriter("data.xml",null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
}
}
直接有操作xml的类,如上面说的,这个只是一个小示例顺便说哈,如果你不是另外写一个文件(配置),那么可以考虑哈用项目自带的配置文件,你只需要添加字段就可以了,相当简单
<employee>
<id>xiaohong </name>
<name>female </sex>
<pwd>IT </dept>
</employee>
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication2\XMLFile3.xml");
XmlNode node = doc.SelectSingleNode("/employee");
string id= node["id"];
string name= node["name"];
将 XML 文档读入 DOM
请参见
语言筛选器: 全部 语言筛选器: 多个 语言筛选器: Visual Basic 语言筛选器: C# 语言筛选器: C++ 语言筛选器: J# 语言筛选器: JScript
Visual Basic(声明)
Visual Basic(用法)
C#
C++
J#
JScript XML 信息从不同的格式读入内存。读取源包括字符串、流、URL、文本读取器或 XmlReader 的派生类。Load 方法将文档置入内存中并包含可用于从每个不同的格式中获取数据的重载方法。还存在 LoadXml 方法,该方法从字符串中读取 XML。不同的 Load 方法影响在加载 XML 文档对象模型 (DOM) 时创建的节点。下表列出了一些 Load 方法的区别以及讲述这些区别的主题。课题 主题
创建空白节点
用来加载 DOM 的对象对 DOM 中生成的空白和有效空白节点有影响。有关更多信息,请参见加载 DOM 时的空白和有效空白处理。
从特定节点开始加载 XML 或加载整个 XML 文档
使用 System.Xml.XmlDocument.Load(System.Xml.XmlReader) 方法可以将数据从特定的节点加载到 DOM 中。有关更多信息,请参见从读取器中加载数据。
在 XML 加载时进行验证
加载到 DOM 中的 XML 数据可以在加载时进行验证。使用验证 XmlReader 可以完成此操作。有关在 XML 加载时进行验证的更多信息,请参见在 DOM 中验证 XML 文档。
以下示例显示使用 LoadXml 方法加载的 XML 以及之后保存到称为 data.xml 的文本文件的数据。using System;
using System.IO;
using System.Xml;public class Sample
{
public static void Main()
{
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>"); // Save the document to a file.
doc.Save("data.xml");
}
}
请参见
概念
XML 文档对象模型 (DOM)
要提出有关“帮助”或本产品其他功能的建议或错误报告,请转到反馈站点。
string filename=@"c:\temp\info.xml";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" "); try
{
if (!File.Exists(filename))
{
File.Create(filename);
}
doc.Load(filename);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
} using (XmlWriter writer = XmlWriter.Create(filename, settings))
{ writer.WriteStartElement("Information"); writer.WriteStartElement("IP");
writer.WriteStartAttribute("Address"); writer.WriteString("10.72.68.60");
writer.WriteEndAttribute();
writer.WriteEndElement();
writer.WriteStartElement("User");
writer.WriteStartAttribute("Name");
writer.WriteString("用户名");
writer.WriteEndAttribute(); writer.WriteStartAttribute("Password");
writer.WriteString("pwd");
writer.WriteEndAttribute();
writer.WriteEndElement(); writer.Flush();
}
<ComputerInfo>
<Computer1>
<IP>10.20.32.50</IP>
<USERNAME>administrator</USERNAME>
<PWD>123456</PWD>
</Computer1>
<Computer2>
<IP>10.20.32.51</IP>
<USERNAME>administrator</USERNAME>
<PWD>123456</PWD>
</Computer2>
</ComputerInfo> C#CODE
XmlDocument Doc = new XmlDocument();
Doc.Load(Application .StartupPath + "\\ComputerInfo.xml");
XmlNodeList MyNodes = Doc.GetElementsByTagName("IP");
....
依此类推
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;namespace WaveDBMS.TestGV
{
public partial class AjaxExampleGV : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XMLFile.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet("my");
ds.ReadXml(Server.MapPath("XMLFile.xml"));//读取xml文件
DataRow dr = ds.Tables[0].NewRow();
dr["name"] = TextBox1.Text.ToString();
dr["couent"] = TextBox2.Text.ToString();
dr["sdate"] = DateTime.Now;
ds.Tables[0].Rows.Add(dr);
ds.WriteXml(Server.MapPath("XMLFile.xml"));//写入xml文件
//Response.Redirect("AjaxExampleGV.aspx");
BindData();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
}
}aspx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxExampleGV.aspx.cs" Inherits="WaveDBMS.TestGV.AjaxExampleGV" %><%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" CellPadding="0" ForeColor="#333333" GridLines="None" AllowPaging="True" AutoGenerateColumns="False" Width="100%" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5" BorderStyle="Solid" BorderWidth="1px">
<Columns>
<asp:TemplateField>
<HeaderStyle Width="100%" />
<ItemTemplate>
<table border="0" cellpadding="2" cellspacing="1">
<tr>
<td>用户姓名:<%#Eval("name") %>留言时间:<%#Eval("sdate")%></td>
</tr>
<tr><td><hr width="100%" color="green"/></td></tr>
<tr><td>留言内容:<%#Eval("couent")%></td></tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Right" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<table>
<tr><td style="width: 306px">姓名
<asp:TextBox ID="TextBox1" runat="server" Width="144px" ></asp:TextBox></td></tr>
<tr><td style="width: 306px; height: 77px"> 留言内容
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Width="223px" Height="109px"></asp:TextBox></td></tr>
<tr><td colspan="2"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交" SkinID="o" /></td></tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>