2012-03-02 4 views
3

次のプログラムを使用して、C#を使用してxmlドキュメントに格納されているデータを更新しています。C#を使用してXMLに格納されたデータを更新するには?

usernamepasswordの2つのフィールドがあります。データを挿入しようとすると、正常に追加されます。

私の問題は、すでにXML文書に格納されているデータを更新しようとするときです。レコードを更新できません。では、C#を使用してXLMドキュメントのレコードをどのように更新できますか?

私は、この行で発信され、次の例外NullReferenceExceptionあります

root.ReplaceChild(newCd, oldCd); 

Button1_ClickButton2_Clickがデータを更新し、データを追加します。

using System; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Xml; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     Connection();  
    } 

    protected void Connection() 
    { 
     XmlDocument xmldoc = new XmlDocument(); 
     xmldoc.Load(@"D:\vijay.net\xmlstorage\XMLFile.xml"); 
     XmlNode xmlnod = xmldoc.SelectSingleNode("records"); 
     XmlNode xmlrec = xmlnod.AppendChild(xmldoc.CreateNode(XmlNodeType.Element, "record", "")); 
     xmlrec.AppendChild(xmldoc.CreateNode(XmlNodeType.Element, "Username", "")).InnerText = TextBox1.Text; 
     xmlrec.AppendChild(xmldoc.CreateNode(XmlNodeType.Element, "password", "")).InnerText = TextBox2.Text; 
     xmldoc.Save(@"D:\vijay.net\xmlstorage\XMLFile.xml"); 
     Response.Write("Successfully saved in xml file"); 
     TextBox1.Text = ""; 
     TextBox2.Text = ""; 
    } 

    protected void Button2_Click(object sender, EventArgs e) 
    {  
     XmlTextReader reader = new XmlTextReader(@"D:\vijay.net\xmlstorage\XMLFile.xml"); 
     XmlDocument doc = new XmlDocument(); 
     doc.Load(reader); 
     reader.Close(); 

     XmlNode oldCd; 
     XmlElement root = doc.DocumentElement; 
     oldCd = root.SelectSingleNode("/catalog/cd[Username='" + TextBox1.Text + "']"); 
     XmlElement newCd = doc.CreateElement("cd"); 
     newCd.SetAttribute("Password", TextBox2.Text); 

     newCd.InnerXml = "<Username>" + this.TextBox1.Text + "</Username>"; 
     root.ReplaceChild(newCd, oldCd); 
     doc.Save(@"D:\vijay.net\xmlstorage\XMLFile.xml"); 
    } 
} 
+0

あなたはXmlDataSourceコントロールを見てきました、それはあなたのためにすべての作業を行います... http://msdn.microsoft。 com/ja/us/library/system.web.ui.webcontrols.xmldatasource.aspx – Lloyd

答えて

1

問題は、XML文書と変更するノードを選択するために使用するXPath式間非互換性のようです。結果のXML構造は次のようなものになりますConnection()メソッドのコードに基づいて

Button2_Clickイベントハンドラで

<records> 
    <record> 
     <username>SomeUsername</username> 
     <password>SomePassword</password> 
    </record> 
</records> 

を、あなたはそのUsername属性の値に基づいて/catalog/cdノードを選択しています:それは、実際のXML構造を反映していないので、nullを返し

oldCd = root.SelectSingleNode("/catalog/cd[Username='" + TextBox2.Text + "']"); 

。代わりに、その内容に基づいてoldCdノードを選択するために、XPath式を変更する必要があります。

oldCd = root.SelectSingleNode("//username[contains(., '" + TextBox2.Text + "')]"); 
+0

oldCd = root.SelectSingleNode( "// username [(。、" "+ TextBox2.Text +" ')] ");私はこのように使用する場合、私は同じエラーに直面している。 – user

+0

実際にロードされているXMLを投稿できますか? –

関連する問題