[ASP.NET C# - XML] - Ajout
Bonsoir tout le monde,
J'ai suit un tutorial pour créer une page ASP .NET qui sert à écrire dans un fichier XML,
Je n'ai aucune erreur lors de l'éxécution mais rien n'est écrit sur ma page XML!!!
En tout cas il me semble que le problème est au niveau de l'entete de la page .aspx
Merci pour l'aide,
ajoutxml.aspx
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="ajoutxml.aspx.cs" Inherits="xml2.ajoutxml" %>
<!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 id="Head1" runat="server">
<title>Ajout XML</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR" />
<meta content="C#" name="CODE_LANGUAGE" />
<meta content="JavaScript (ECMAScript)" name="vs_defaultClientScript" />
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema" />
</head>
<body>
<form id="EditCustomer" runat="server" method="post">
<div>
<table width="640">
<tbody>
<tr>
<td align="middle" colspan="2">
<p>
<font face="Verdana,Arial" color="#3300ff" size="4">Ajout d'un champs</font>
</p>
</td>
</tr>
<tr>
<td align="right" width="30%">
<font face="Verdana,Arial" color="#3300ff" size="2">ID: </font></td>
<td>
<asp:TextBox id="ID" runat="server" Width="50px" MaxLength="3"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator0" runat="server" ErrorMessage="*" ControlToValidate="ID"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" width="30%">
<font face="Verdana,Arial" color="#3300ff" size="2">Titre: </font></td>
<td>
<asp:TextBox id="Titre" runat="server" Width="259px" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="Titre"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" width="30%">
<font face="Verdana,Arial" color="#3300ff" size="2">Artist: </font></td>
<td>
<asp:TextBox id="Artist" runat="server" Width="259px" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate="Artist"></asp:RequiredFieldValidator>
<p>
</p>
</td>
</tr>
<tr>
<td align="right" width="30%">
<font face="Verdana,Arial" color="#3300ff" size="2">country: </font></td>
<td>
<asp:TextBox id="country" runat="server" Width="259px" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server" ErrorMessage="*" ControlToValidate="country"></asp:RequiredFieldValidator>
<p>
</p>
</td>
</tr>
<tr>
<td align="right" width="30%">
<font face="Verdana,Arial" color="#3300ff" size="2">company: </font></td>
<td>
<asp:TextBox id="company" runat="server" Width="259px" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator4" runat="server" ErrorMessage="*" ControlToValidate="company"></asp:RequiredFieldValidator>
<p>
</p>
</td>
</tr>
<tr>
<td align="right" width="30%">
<font face="Verdana,Arial" color="#3300ff" size="2">price: </font></td>
<td>
<asp:TextBox id="price" runat="server" Width="259px" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator5" runat="server" ErrorMessage="*" ControlToValidate="price"></asp:RequiredFieldValidator>
<p>
</p>
</td>
</tr>
<tr>
<td align="right" width="30%">
<font face="Verdana,Arial" color="#3300ff" size="2">year: </font></td>
<td>
<asp:TextBox id="year" runat="server" Width="259px" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator6" runat="server" ErrorMessage="*" ControlToValidate="year"></asp:RequiredFieldValidator>
<p>
</p>
</td>
</tr>
<tr>
<td align="middle" colspan="2">
<asp:Button id="BtnValid" runat="server" Text="Valid"></asp:Button>
<asp:Button id="BtnCancel" runat="server" Width="50px" Text="Cancel" CausesValidation="False"></asp:Button>
</td>
</tr>
</tbody>
</table>
<p>
</p>
<p>
<asp:Label id="Label1" runat="server" forecolor="Red" font-names="Verdana,Arial"></asp:Label>
</p>
<p>
<asp:Label id="Label2" runat="server" forecolor="black" font-names="Verdana,Arial"></asp:Label>
</p>
</div>
</form>
<!-- Insert content here -->
</body>
</html> |
ajoutxml.aspx.cs
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
namespace xml2
{
public partial class ajoutxml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public class Ajout : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Repeater repeater1;
protected System.Web.UI.WebControls.TextBox ID;
protected System.Web.UI.WebControls.TextBox Titre;
protected System.Web.UI.WebControls.TextBox Artist;
protected System.Web.UI.WebControls.TextBox country;
protected System.Web.UI.WebControls.TextBox company;
protected System.Web.UI.WebControls.TextBox price;
protected System.Web.UI.WebControls.TextBox year;
protected System.Web.UI.WebControls.Button BtnValid;
protected System.Web.UI.WebControls.Button BtnCancel;
public int ID_LIBELLE
{
get { return (int)ViewState["ID_LIB"]; }
set { ViewState["ID_LIB"] = value; }
}
public Ajout()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void doDataBind()
{
try
{
this.DataBind();
}
catch (System.Exception eLoad)
{
// Handle it...
Label1.Text = eLoad.Message;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!(this.IsPostBack))
{
doDataBind();
}
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
#region Web Form Designer generated code
private void InitializeComponent()
{
this.BtnValid.Click += new System.EventHandler(this.BtnValid_Click);
this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void BtnCancel_Click(object sender, System.EventArgs e)
{
Response.Redirect("xmlrepeater.aspx");
}
private void BtnValid_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
if (this.IsValid)
{
try
{
xmlDoc.Load(Server.MapPath("cdcatalog.xml"));
// Create a new <Cd> element and add it to the root node
XmlElement parentNode = xmlDoc.CreateElement("cd");
// Set attribute name and value!
parentNode.SetAttribute("ID", ID.Text);
xmlDoc.DocumentElement.PrependChild(parentNode);
// Create the required nodes
XmlElement titleNode = xmlDoc.CreateElement("title");
XmlElement artistNode = xmlDoc.CreateElement("artist");
XmlElement countryNode = xmlDoc.CreateElement("country");
XmlElement companyNode = xmlDoc.CreateElement("company");
XmlElement priceNode = xmlDoc.CreateElement("price");
XmlElement yearNode = xmlDoc.CreateElement("year");
// retrieve the text
XmlText titleText = xmlDoc.CreateTextNode(Titre.Text);
XmlText artistText = xmlDoc.CreateTextNode(Artist.Text);
XmlText countryText = xmlDoc.CreateTextNode(country.Text);
XmlText companyText = xmlDoc.CreateTextNode(company.Text);
XmlText priceText = xmlDoc.CreateTextNode(price.Text);
XmlText yearText = xmlDoc.CreateTextNode(year.Text);
// append the nodes to the parentNode without the value
parentNode.AppendChild(titleNode);
parentNode.AppendChild(artistNode);
parentNode.AppendChild(countryNode);
parentNode.AppendChild(companyNode);
parentNode.AppendChild(priceNode);
parentNode.AppendChild(yearNode);
// save the value of the fields into the nodes
titleNode.AppendChild(titleText);
artistNode.AppendChild(artistText);
countryNode.AppendChild(countryText);
companyNode.AppendChild(companyText);
priceNode.AppendChild(priceText);
yearNode.AppendChild(yearText);
// Save to the XML file
XmlNodeList nodeList = xmlDoc.SelectNodes("/catalog/cd[@ID=" + ID.Text + "]");
if (nodeList.Count == 1)
{
xmlDoc.Save(Server.MapPath("cdcatalog.xml"));
Response.Redirect("xmlrepeater.aspx");
}
else
{
Response.Write("Impossible d'ajouter !!!, L'ID déja existant");
}
ID.Text = "";
Titre.Text = "";
Artist.Text = "";
country.Text = "";
company.Text = "";
price.Text = "";
year.Text = "";
}
catch (System.Exception eSave)
{
Label1.Text = eSave.Message;
//Label2.Text=message;
}
}
}
}
}
} |