2014-07-02 41 views
-1

我正在使用xml,並且必須在xml中添加新標記。 以下XML是:將屬性添加到C#中的現有XMLNode中

<LoginDetails> 
    <Login> 
    <FirstName>Abhishek</FirstName> 
    <LastName>Pathak</LastName> 
    <securityQuestion>What is you DOB</securityQuestion> 
    <SecAnswer>31/03/1985</SecAnswer> 
    <LoginId>abc</LoginId> 
    <Password>1234</Password> 
    </Login> 

</LoginDetails> 

,我要添加新的標籤,如:

<Login> 
    <FirstName>vivek</FirstName> 
    <LastName>sharma</LastName> 
    <securityQuestion>What is you DOB</securityQuestion> 
    <SecAnswer>27/03/1985</SecAnswer> 
    <LoginId>abcd</LoginId> 
    <Password>3214</Password> 
    </Login> 

能ANY1給我在C#代碼。

+0

我不認爲這個網站的人要求的代碼片段... –

回答

0

所以最後我能做到這一點myselft:

XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.Load(@"D:\abhishek\LoginDetails1.xml"); 

    XmlElement elmRoot = xmlDoc.DocumentElement; 
    XmlElement elmNew = xmlDoc.CreateElement("Login"); 
    elmRoot.AppendChild(elmNew); 


    elmRoot = xmlDoc.DocumentElement; 
    elmNew = xmlDoc.CreateElement("FirstName"); 
    XmlText txtVideo = xmlDoc.CreateTextNode(txtFname.Text); 
    elmRoot.LastChild.AppendChild(elmNew); 
    elmRoot.LastChild.LastChild.AppendChild(txtVideo); 

    elmRoot = xmlDoc.DocumentElement; 
    elmNew = xmlDoc.CreateElement("LastName"); 
    txtVideo = xmlDoc.CreateTextNode(txtLastName.Text); 
    elmRoot.LastChild.AppendChild(elmNew); 
    elmRoot.LastChild.LastChild.AppendChild(txtVideo); 

    elmRoot = xmlDoc.DocumentElement; 
    elmNew = xmlDoc.CreateElement("securityQuestion"); 
    txtVideo = xmlDoc.CreateTextNode(comboBox.SelectedValue.ToString()); 
    elmRoot.LastChild.AppendChild(elmNew); 
    elmRoot.LastChild.LastChild.AppendChild(txtVideo); 

    elmRoot = xmlDoc.DocumentElement; 
    elmNew = xmlDoc.CreateElement("SecAnswer"); 
    txtVideo = xmlDoc.CreateTextNode(txtAnswer.Text); 
    elmRoot.LastChild.AppendChild(elmNew); 
    elmRoot.LastChild.LastChild.AppendChild(txtVideo); 
    elmRoot = xmlDoc.DocumentElement; 
    elmNew = xmlDoc.CreateElement("LoginId"); 
    txtVideo = xmlDoc.CreateTextNode(txtLoginId.Text); 
    elmRoot.LastChild.AppendChild(elmNew); 
    elmRoot.LastChild.LastChild.AppendChild(txtVideo); 

    elmRoot = xmlDoc.DocumentElement; 
    elmNew = xmlDoc.CreateElement("Password"); 
    txtVideo = xmlDoc.CreateTextNode(txtPwd.Text); 
    elmRoot.LastChild.AppendChild(elmNew); 
    elmRoot.LastChild.LastChild.AppendChild(txtVideo); 

    xmlDoc.Save(@"D:\abhishek\LoginDetails.xml");