xml文件代码:C# 对XML文件的操作代码



找了很久都没找到C# 对XML文件操作代码终于找到了呵呵希望能对新手有帮助
//-------------------------------------------html 部分
<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"XML.aspx.cs\" Inherits=\"XML\" %>
<!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>XML</title>
<script language=\"javascript\" type=\"text/javascript\">
function check
{
(document.getElementById(\"img_url\").value\"\")
{
document.getElementById(\"img_url\").focus;
alert(’图片地址不能为空!’);
false;
}
(document.getElementById(\"url\").value\"\")
{
alert(’广告链接地址不能为空!’);
document.getElementById(\"url\").focus;
false;
}
(document.getElementById(\"AIT\").value\"\")
{
alert(’图片替换文本不能为空!’);
document.getElementById(\"AIT\").focus;
false;
}
(document.getElementById(\"title\").value\"\")
{
alert(’标题不能为空!’);
document.getElementById(\"title\").focus;
false;
}
(document.getElementById(\"ganlv\").value\"\")
{
alert(’概率不能为空!’);
document.getElementById(\"ganlv\").focus;
false;
}
var str = document.getElementById(\"ganlv\").value;
var standard = \"0123456789\";
for (var i = 0; i < str.length; i)
{
(standard.search(str.charAt(i)) -1) [Page]
{
alert(\"价格中请输入数字.\");
document.getElementById(\"ganlv\").value’’;
document.getElementById(\"ganlv\").focus;
false;
}
}
true;
}


function check2
{
(document.getElementById(\"img_url2\").value\"\")
{
document.getElementById(\"img_url2\").focus;
alert(’图片地址不能为空!’);
false;
}
(document.getElementById(\"url2\").value\"\")
{
alert(’广告链接地址不能为空!’);
document.getElementById(\"url2\").focus;
false;
}
(document.getElementById(\"AIT2\").value\"\")
{
alert(’图片替换文本不能为空!’);
document.getElementById(\"AIT2\").focus;
false;
}
(document.getElementById(\"title2\").value\"\")
{


alert(’标题不能为空!’);
document.getElementById(\"title2\").focus;
false;
}
(document.getElementById(\"ganlv2\").value\"\")
{
alert(’概率不能为空!’);
document.getElementById(\"ganlv2\").focus;
false;
} [Page]
var str = document.getElementById(\"ganlv2\").value;
var standard = \"0123456789\";
for (var i = 0; i < str.length; i)
{
(standard.search(str.charAt(i)) -1)
{
alert(\"价格中请输入数字.\");
document.getElementById(\"ganlv2\").value’’;
document.getElementById(\"ganlv2\").focus;
false;
}
}
true;
}
</script>
<script type=\"text/javascript\" language=\"javascript\">
var last = null;
function judge(obj)
{
(last null)
{
last = obj.id;
// alert(last);
}

{
var lo = document.getElementById(last);
lo.checked = \"\";
// alert(last + \" \" + lo.checked);
last = obj.name;
}
obj.checked = \"checked\";
} [Page]
</script>
</head>
<body style=\"font-size: 12px; margin: 0px\">
<form id=\"form1\" runat=\"server\">
<div>
<div style=\"text-align: center\">
&nbsp;<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width: 600px\">
<tr>
<td style=\"height: 60px\">
</td>
</tr>
<tr>
<td style=\"height: 154px;\">
<anthem:GridView ID=\"GridView1\" runat=\"server\" AutoGenerateColumns=\"False\" BackColor=\"White\"
BorderColor=\"#3366CC\" BorderStyle=\"None\" BorderWidth=\"1px\" CellPadding=\"4\" Width=\"854px\" _disibledevent=> <FooterStyle BackColor=\"#99CCCC\" ForeColor=\"#003399\" />


<PagerStyle BackColor=\"#99CCCC\" ForeColor=\"#003399\" HorizontalAlign=\"Left\" />
<HeaderStyle BackColor=\"#003399\" Font-Bold=\"True\" ForeColor=\"#CCCCFF\" />
<SelectedRowStyle BackColor=\"#009999\" Font-Bold=\"True\" ForeColor=\"#CCFF99\" /> [Page]
<Columns>
<asp:TemplateField>
<itemtemplate>
<anthem:RadioButton id=\"RadioButton1\" runat=\"server\" __designer:wfdid=\"w31\"></anthem:RadioButton>&nbsp;
</itemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=\"图片地址\">
<itemtemplate>
<anthem:Label id=\"Label1\" runat=\"server\" __designer:wfdid=\"w3\" Text=’<%# DataBinder.Eval(Container.DataItem,\"imageUrl\") %>’></anthem:Label>
</itemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=\"链接地址\">
<itemtemplate>
<anthem:Label id=\"Label2\" runat=\"server\" __designer:wfdid=\"w5\" Text=’<%# DataBinder.Eval(Container.DataItem,\"NavigateUrl\") %>’></anthem:Label>
</itemtemplate> [Page]
</asp:TemplateField>
<asp:TemplateField HeaderText=\"概率\">
<itemtemplate>
<anthem:Label id=\"Label3\" runat=\"server\" __designer:wfdid=\"w6\" Text=’<%# DataBinder.Eval(Container.DataItem,\"Impressions\") %>’></anthem:Label>
</itemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=\"图片替换\">
<itemtemplate>
<anthem:Label id=\"Label4\" runat=\"server\" __designer:wfdid=\"w7\" Text=’<%# DataBinder.Eval(Container.DataItem,\"AlternateText\") %>’></anthem:Label>


</itemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=\"标题\">
<itemtemplate>
<anthem:Label id=\"Label5\" runat=\"server\" __designer:wfdid=\"w9\" Text=’<%# DataBinder.Eval(Container.DataItem,\"KeyWord\") %>’></anthem:Label> [Page]
</itemtemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor=\"White\" ForeColor=\"#003399\" />
</anthem:GridView>
</td>
</tr>
<tr>
<td align=\"left\" style=\"height: 15px\">
</td>
</tr>
<tr>
<td align=\"left\">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<anthem:Button ID=\"Button2\" runat=\"server\" _disibledevent=> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<anthem:Button ID=\"Button4\" runat=\"server\" _disibledevent=> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
<anthem:Button ID=\"Button7\" runat=\"server\" _disibledevent=> </tr>
<tr>
<td align=\"left\" style=\"height: 193px\">
&nbsp;<anthem:Panel ID=\"Panel1\" runat=\"server\" Visible=\"False\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width: 787px\">
<tr>


<td style=\"width: 100px; height: 15px\">
增加条广告:</td>
<td style=\"width: 87px; height: 15px\">
图片地址:</td> [Page]
<td style=\"width: 100px; height: 15px\">
<anthem:TextBox ID=\"img_url\" runat=\"server\" BorderStyle=\"Groove\" Width=\"573px\"></anthem:TextBox></td>
</tr>
<tr>
<td rowspan=\"4\" style=\"width: 100px\">
</td>
<td style=\"width: 87px; height: 15px\">
链接地址:</td>
<td style=\"width: 100px; height: 15px\">
<anthem:TextBox ID=\"url\" runat=\"server\" BorderStyle=\"Groove\" Width=\"572px\"></anthem:TextBox></td> [Page]
</tr>
<tr>
<td style=\"width: 87px\">
图片替换文本:</td>
<td style=\"width: 100px\">
<anthem:TextBox ID=\"AIT\" runat=\"server\" BorderStyle=\"Groove\" Width=\"570px\"></anthem:TextBox></td>


</tr>
<tr>
<td style=\"width: 87px; height: 24px\">
标 &nbsp; &nbsp; &nbsp;&nbsp; 题:</td>
<td style=\"width: 100px; height: 24px\"> [Page]
<anthem:TextBox ID=\"title\" runat=\"server\" BorderStyle=\"Groove\" Width=\"570px\"></anthem:TextBox></td>
</tr>
<tr>
<td style=\"width: 87px; height: 24px\">
概 &nbsp; &nbsp; &nbsp;&nbsp; 率:</td>
<td style=\"width: 100px; height: 24px\">
<anthem:TextBox ID=\"ganlv\" runat=\"server\" BorderStyle=\"Groove\"></anthem:TextBox></td>
</tr>
<tr>
<td rowspan=\"1\" style=\"width: 100px; height: 15px\"> [Page]
</td>
<td align=\"center\" colspan=\"2\" style=\"height: 15px\">
<anthem:Button ID=\"Button1\" runat=\"server\" _disibledevent=> &nbsp; &nbsp; &nbsp;
<anthem:Button ID=\"Button3\" runat=\"server\" _disibledevent=>


</tr>
</table>
</anthem:Panel>
<anthem:Panel ID=\"Panel3\" runat=\"server\" Visible=\"False\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width: 787px\">
<tr> [Page]
<td style=\"width: 100px; height: 15px\">
修改条广告:</td>
<td style=\"width: 92px; height: 15px\">
图片地址:</td>
<td style=\"width: 100px; height: 15px\">
<anthem:TextBox ID=\"img_url2\" runat=\"server\" BorderStyle=\"Groove\" Width=\"585px\"></anthem:TextBox></td>
</tr>
<tr>
<td rowspan=\"4\" style=\"width: 100px\">
</td> [Page]
<td style=\"width: 92px; height: 15px\">
链接地址:</td>
<td style=\"width: 100px; height: 15px\">
<anthem:TextBox ID=\"url2\" runat=\"server\" BorderStyle=\"Groove\" Width=\"584px\"></anthem:TextBox></td>
</tr>


<tr>
<td style=\"width: 92px\">
图片替换文本:</td>
<td style=\"width: 100px\">
<anthem:TextBox ID=\"AIT2\" runat=\"server\" BorderStyle=\"Groove\" Width=\"580px\"></anthem:TextBox></td> [Page]
</tr>
<tr>
<td style=\"width: 92px; height: 24px\">
标 &nbsp; &nbsp; &nbsp;&nbsp; 题:</td>
<td style=\"width: 100px; height: 24px\">
<anthem:TextBox ID=\"title2\" runat=\"server\" BorderStyle=\"Groove\" Width=\"581px\"></anthem:TextBox></td>
</tr>
<tr>
<td style=\"width: 92px; height: 24px\">
概 &nbsp; &nbsp; &nbsp;&nbsp; 率:</td> [Page]
<td style=\"width: 100px; height: 24px\">
<anthem:TextBox ID=\"ganlv2\" runat=\"server\" BorderStyle=\"Groove\"></anthem:TextBox></td>
</tr>
<tr>
<td rowspan=\"1\" style=\"width: 100px; height: 15px\">


</td>
<td align=\"center\" colspan=\"2\" style=\"height: 15px\">
<anthem:Button ID=\"Button5\" runat=\"server\" _disibledevent=> &nbsp; &nbsp; &nbsp;
<anthem:Button ID=\"Button6\" runat=\"server\" _disibledevent=> </tr>
</table>
</anthem:Panel>
</td>
</tr>
</table>
</div>

</div>
</form>
</body>
</html>
//------------------------------------------------------------cs部分
using ;
using .Data;
using .Configuration;
using .Collections;
using .Web;
using .Web.Security;
using .Web.UI;
using .Web.UI.WebControls;
using .Web.UI.WebControls.WebParts;
using .Web.UI.HtmlControls;
using .Xml;
using .IO;
using .Text;
using .Xml.XPath;
using .Xml.Schema;
using .Xml.Serialization;
using .Xml.Xsl;
using Anthem;
public partial XML : .Web.UI.Page
{
XmlDataDocument dt = XmlDataDocument;
protected void Page_Load(object sender, EventArgs e)
{
(!Page.IsPostBack)
{
this.Bind_Xml;
Button1.Attributes.Add(\"onclick\",\" check\");
Button5.Attributes.Add(\"onclick\",\" check2\");
}
}
public void Bind_Xml
{
filename = \"adrot.xml\";
try
{
StreamReader srd = StreamReader(Server.MapPath(filename), .Text.Encoding.GetEncoding(\"GB2312\")); [Page]
dt.DataSet.ReadXml(srd);
this.GridView1.DataSource = dt.DataSet.Tables[0].DefaultView;
this.GridView1.DataBind;
this.GridView1.UpdateAfterCallBack = true;
dt = null;
srd.Close;
}
catch(Exception ex)
{
throw Exception(ex.Message,ex);


}

}
protected void Button2_Click(object sender, EventArgs e)
{
this.Panel1.Visible = true;
this.Panel1.UpdateAfterCallBack = true;
this.Panel3.Visible = false;
this.Panel3.UpdateAfterCallBack = true;
}
protected void Button3_Click(object sender, EventArgs e)
{
this.Panel1.Visible = false;
this.Panel1.UpdateAfterCallBack = true;
this.Panel3.Visible = false;
this.Panel3.UpdateAfterCallBack = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
// -----------------------------------------增加XML内容
(IsValid true)
{
filename = \"adrot.xml\";
try
{
XmlDocument XmlDoc = XmlDocument;
XmlDoc.Load(Server.MapPath(filename)); [Page]
XmlNode root = XmlDoc.SelectSingleNode(\"Advertisements\");
XmlElement Xel = XmlDoc.CreateElement(\"Ad\");
XmlElement Xel1 = XmlDoc.CreateElement(\"imageUrl\");
Xel1.InnerText = this.img_url.Text;
Xel.AppendChild(Xel1);
XmlElement Xel2 = XmlDoc.CreateElement(\"NavigateUrl\");
Xel2.InnerText = this.url.Text;
Xel.AppendChild(Xel2);
XmlElement Xel3 = XmlDoc.CreateElement(\"AlternateText\");
Xel3.InnerText = this.AIT.Text;
Xel.AppendChild(Xel3);
XmlElement Xel4 = XmlDoc.CreateElement(\"KeyWord\");
Xel4.InnerText = this.title.Text;
Xel.AppendChild(Xel4);
XmlElement Xel5 = XmlDoc.CreateElement(\"Impressions\");
Xel5.InnerText = this.ganlv.Text;
Xel.AppendChild(Xel5);
root.AppendChild(Xel);
XmlDoc.Save(Server.MapPath(filename));
this.Bind_Xml; [Page]
}
catch (Exception ex)
{
throw Exception(ex.Message, ex);
}
this.Panel1.Visible = false;
this.Panel1.UpdateAfterCallBack = true;


}
}
protected void Button4_Click(object sender, EventArgs e)
{
this.Panel1.Visible = false;
this.Panel1.UpdateAfterCallBack = true;
this.Panel3.Visible = true;
foreach (GridViewRow row in GridView1.Rows)
{
bool IsCheckBox = ((Anthem.RadioButton)row.Cells[0].FindControl(\"RadioButton1\")).Checked;
(IsCheckBox true)
{
this.img_url2.Text = ((Anthem.Label)row.Cells[1].FindControl(\"Label1\")).Text;
this.url2.Text = ((Anthem.Label)row.Cells[2].FindControl(\"Label2\")).Text;
this.AIT2.Text = ((Anthem.Label)row.Cells[4].FindControl(\"Label4\")).Text;
this.title2.Text = ((Anthem.Label)row.Cells[5].FindControl(\"Label5\")).Text;
this.ganlv2.Text = ((Anthem.Label)row.Cells[3].FindControl(\"Label3\")).Text;
}
}
this.Panel3.UpdateAfterCallBack = true;
} [Page]
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
(e.Row.RowType DataControlRowType.DataRow)
{
Anthem.RadioButton rb = (Anthem.RadioButton)e.Row.FindControl(\"RadioButton1\");
rb.Attributes.Add(\"onclick\", \"judge(this)\");
}
}
protected void Button6_Click(object sender, EventArgs e)
{
this.Panel3.Visible = false;
this.Panel3.UpdateAfterCallBack = true;
this.Panel1.Visible = false;
this.Panel1.UpdateAfterCallBack = true;
}
protected void Button5_Click(object sender, EventArgs e)
{
//----------------------修改
(IsValid true)
{
foreach (GridViewRow row in GridView1.Rows)
{
url = \"adrot.xml\";
XmlDocument Xmldoc = XmlDocument;
Xmldoc.Load(Server.MapPath(url));
bool IsCheckBox = ((Anthem.RadioButton)row.Cells[0].FindControl(\"RadioButton1\")).Checked;
(IsCheckBox true)
{
XmlNode root = Xmldoc.SelectSingleNode(\"Advertisements\");
XmlNodeList xnl = root.ChildNodes; [Page]
for ( i = 0; i < xnl.Count; i)
{
XmlNodeList xnl11 = xnl.Item(i).SelectNodes(\"imageUrl\");//查找username下面节点集合


XmlElement xe = (XmlElement)xnl11.Item(0); //username节点下面元素
(this.img_url2.Text xe.InnerText)
{
xe.ParentNode.ChildNodes.Item(0).InnerText = this.img_url2.Text;
xe.ParentNode.ChildNodes.Item(1).InnerText = this.url2.Text;
xe.ParentNode.ChildNodes.Item(2).InnerText = this.AIT2.Text;
xe.ParentNode.ChildNodes.Item(3).InnerText = this.title2.Text;
xe.ParentNode.ChildNodes.Item(4).InnerText = this.ganlv2.Text;
}
}
}
Xmldoc.Save(Server.MapPath(\"adrot.xml\")); [Page]
}
this.Panel3.Visible = false;
this.Panel3.UpdateAfterCallBack = true;
this.Bind_Xml;
}
}
protected void Button7_Click(object sender, EventArgs e)
{
//-----------------------删除
foreach (GridViewRow row in GridView1.Rows)
{
bool IsChockBox = ((Anthem.RadioButton)row.Cells[0].FindControl(\"RadioButton1\")).Checked;
XmlDocument XmlDoc = XmlDocument;
XmlDoc.Load(Server.MapPath(\"adrot.xml\"));
(IsChockBox true)
{
XmlNode root = XmlDoc.SelectSingleNode(\"Advertisements\");
XmlNodeList xnl = root.ChildNodes;
for ( i = 1; i < xnl.Count; i)
{
XmlNodeList xnl11 = xnl.Item(i).SelectNodes(\"imageUrl\");
XmlElement xe = (XmlElement)xnl11.Item(0);
(xe.InnerText ((Anthem.Label)row.Cells[1].FindControl(\"Label1\")).Text)
{
root.RemoveChild(xe.ParentNode); [Page]
}
}



}
XmlDoc.Save(Server.MapPath(\"adrot.xml\"));
}
this.Bind_Xml;
}
}

Tags:  xml源代码 c打开文件源代码 xml文件操作 xml文件代码

延伸阅读

最新评论

发表评论