EDN Admin
Well-known member
I need help creating xml file with C#<br/>
<br/>
ok lets say I have the following users.xml file that contains the following<br/>
<br/>
<br/>
<Users><br/>
<br/>
</Users><br/>
<br/>
After executing the C# code below<br/>
<br/>
<pre class="prettyprint public void addUser()
{
string filePaths = "users.xml";
XmlDocument xmlDoc = new XmlDocument();
string user = textBox2.Text; // USER INPUT
if (File.Exists(filePaths))
{
xmlDoc.Load(filePaths);
XmlElement elmRoot = xmlDoc.DocumentElement;
XmlElement elmNew = xmlDoc.CreateElement("User");
elmRoot.AppendChild(elmNew);
elmRoot = xmlDoc.DocumentElement;
elmNew = xmlDoc.CreateElement("Name");
XmlText CreateUserText = xmlDoc.CreateTextNode(user);
elmRoot.LastChild.AppendChild(elmNew);
elmRoot.LastChild.LastChild.AppendChild(CreateUserText);//8
xmlDoc.Save(filePaths);
}
}[/code]
<br/>
<br/>
I get the following xml file<br/>
<br/>
<br/>
<br/>
<Users><br/>
<User> <!-- Generated by C# code above --><br/>
<Name>Eric</Name> <!-- Generated by C# code above --><br/>
</User> <!-- Generated by C# code above --><br/>
</Users><br/>
<br/>
I can keep adding users, so for example if i executed again with another name in textBox2. it would produce<br/>
<br/>
<br/>
<Users><br/>
<User> <!-- Generated by C# code above --><br/>
<Name>Eric</Name> <!-- Generated by C# code above --><br/>
</User> <!-- Generated by C# code above --><br/>
<User> <!-- Generated by C# code above --><br/>
<Name>Michelle</Name> <!-- Generated by C# code above --><br/>
</User> <!-- Generated by C# code above --><br/>
</Users><br/>
<br/>
What I am really asking is how would i modify my C# code so that no repeated names are added to the xml file.For example, if i tried to add Eric again in my C# program, users.xml file should not add the name Eric because the name Eric already exist.<br/>
<br/>
Thanks for your help, in advance.<br/>
View the full article
<br/>
ok lets say I have the following users.xml file that contains the following<br/>
<br/>
<br/>
<Users><br/>
<br/>
</Users><br/>
<br/>
After executing the C# code below<br/>
<br/>
<pre class="prettyprint public void addUser()
{
string filePaths = "users.xml";
XmlDocument xmlDoc = new XmlDocument();
string user = textBox2.Text; // USER INPUT
if (File.Exists(filePaths))
{
xmlDoc.Load(filePaths);
XmlElement elmRoot = xmlDoc.DocumentElement;
XmlElement elmNew = xmlDoc.CreateElement("User");
elmRoot.AppendChild(elmNew);
elmRoot = xmlDoc.DocumentElement;
elmNew = xmlDoc.CreateElement("Name");
XmlText CreateUserText = xmlDoc.CreateTextNode(user);
elmRoot.LastChild.AppendChild(elmNew);
elmRoot.LastChild.LastChild.AppendChild(CreateUserText);//8
xmlDoc.Save(filePaths);
}
}[/code]
<br/>
<br/>
I get the following xml file<br/>
<br/>
<br/>
<br/>
<Users><br/>
<User> <!-- Generated by C# code above --><br/>
<Name>Eric</Name> <!-- Generated by C# code above --><br/>
</User> <!-- Generated by C# code above --><br/>
</Users><br/>
<br/>
I can keep adding users, so for example if i executed again with another name in textBox2. it would produce<br/>
<br/>
<br/>
<Users><br/>
<User> <!-- Generated by C# code above --><br/>
<Name>Eric</Name> <!-- Generated by C# code above --><br/>
</User> <!-- Generated by C# code above --><br/>
<User> <!-- Generated by C# code above --><br/>
<Name>Michelle</Name> <!-- Generated by C# code above --><br/>
</User> <!-- Generated by C# code above --><br/>
</Users><br/>
<br/>
What I am really asking is how would i modify my C# code so that no repeated names are added to the xml file.For example, if i tried to add Eric again in my C# program, users.xml file should not add the name Eric because the name Eric already exist.<br/>
<br/>
Thanks for your help, in advance.<br/>
View the full article