0
私はユーザーが新しいメールアドレスを入力するためのテキストボックスを持っています。 「更新」ボタンをクリックすると、入力したテキストが既存のXMLファイルに新しいエントリを作成します。このxmlファイルは2つのドロップダウンリストを設定するために使用され、ユーザーが入力した新しい更新された項目でドロップダウンリストを絶えず更新する必要があります。xboxファイルの新しいノードに保存されるテキストボックスの値
ので、私に
xmlファイル案内してください..私は次のコードsnipperを試してみましたが、私は方法で苦手:ので、このボタンをクリックすると(たとえば、私は新しいビルダーのエントリーをしたい)
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder>
<id>1</id>
<value>[email protected]</value>
</builder>
<builder>
<id>2</id>
<value>Others</value>
</builder>
</builderemail>
<manageremail>
<manager>
<id>1</id>
<value>[email protected]</value>
</manager>
<manager>
<id>2</id>
<value>Others</value>
</manager>
</manageremail>
</email>
を私はあなたの助け
のための私のcode..manyのおかげで何か問題があると思いprotected void Button1_Click(object sender, EventArgs e)
{
AddNodeToXMLFile("~/App_Data/builderemail.xml", email);
}
public void AddNodeToXMLFile(string XmlFilePath, string NodeNameToAddTo)
{
//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();
//load from file
doc.Load(XmlFilePath);
//create main node
XmlNode node = doc.CreateNode(XmlNodeType.Element, "builder", null);
//create the nodes first child
XmlNode ButtonName = doc.CreateElement("id");
//set the value
ButtonName.InnerText = "1";
//create the nodes second child
XmlNode url = doc.CreateElement("value");
//set the value
url.InnerText = "" + TextBox1.Text;
// add childes to father
node.AppendChild(id);
node.AppendChild(value);
// find the node we want to add the new node to
XmlNodeList l = doc.GetElementsByTagName(NodeNameToAddTo);
// append the new node
l[0].AppendChild(node);
// save the file
doc.Save(XmlFilePath);
}
にAddNodeToXMLFile
メソッドを呼び出します // add children to father
node.AppendChild(ButtonName);
node.AppendChild(url);
をし、あなたのいるXmlNodeListは例外を防ぐために、任意のノードが含まれている場合は、チェックする必要があります:
ありがとう:Dは私の質問を解決しました – jeremychan