473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XML Formatting Issue With Elements

I have the following issue I was hoping someone can help me with. I have an
XML doc in the following format:
<main>
<friend value="friend1">
<email>fr*****@mailserver.com</email>
</friend>
<friend value="friend2">
<email>fr*****@mailserver.com</email>
</friend>
</main>

My problem is when using the XMLElement class I have trouble adding the 2
elements to the document in the format <friend
attribute><email>InnerText</email></friend. My code is:

try
{

mydoc = new XmlDocument();
mydoc.Load(Directory.GetCurrentDirectory() +
"\\friends.xml");

XmlNode mynode = mydoc.DocumentElement;

XmlElement elem = mydoc.CreateElement("friend");

elem.SetAttribute("value", "friend3");
XmlElement elem2 = mydoc.CreateElement("email");
elem2.InnerText = "fr*****@ossiusa.com";

mynode.AppendChild(elem);
mynode.AppendChild(elem2);
mydoc.Save(Directory.GetCurrentDirectory() +
"\\friends.xml");

This is causing the child element "email" to append after the closing of
"friend" rather than being a child of the "friend" element. How can I get
email to be the child of friend. Thanks.
--
TC
Feb 2 '07 #1
2 1652
Terrance wrote:
XmlElement elem = mydoc.CreateElement("friend");

elem.SetAttribute("value", "friend3");
XmlElement elem2 = mydoc.CreateElement("email");
elem2.InnerText = "fr*****@ossiusa.com";
elem.AppendChild(elem2);
is what you want.
Then
mynode.AppendChild(elem);
mydoc.Save(Directory.GetCurrentDirectory() +
"\\friends.xml");

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 2 '07 #2
Thanks, that worked. I can't believe I missed that. Thanks, for all your help.
--
TC
"Martin Honnen" wrote:
Terrance wrote:
XmlElement elem = mydoc.CreateElement("friend");

elem.SetAttribute("value", "friend3");
XmlElement elem2 = mydoc.CreateElement("email");
elem2.InnerText = "fr*****@ossiusa.com";

elem.AppendChild(elem2);
is what you want.
Then
mynode.AppendChild(elem);

mydoc.Save(Directory.GetCurrentDirectory() +
"\\friends.xml");


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Darin McGrew | last post by:
Eric Meyer's <http://www.meyerweb.com/eric/css/inline-format.html> is very clear that it is not a simple introduction to the inline formatting model. Unfortunately, that's what I think I need. I...
2
by: Megan | last post by:
Can you write conditional VBA code that affects only one or two records on a continuous subform? I have a form with a subform on it. The parent/ child field that links the forms is CaseID. The...
2
by: Von Bailey | last post by:
I have a form where the conditional formatting is set on some fields to bold if certain conditions are met. However, when the conditions are met some of the data that is to bold is either not...
9
by: sck10 | last post by:
Hello, I have a page with an ImageButton that is used to redirect to another page. When the page first opens, everything looks as expected. However, when I click on the image, the new page...
8
by: gopal | last post by:
Hi I am trying to write to log file but the formatting of string is not properly aligned in log file result The results looks some thing like this OK move.xml Successful OK ...
10
by: hendedav | last post by:
Gang, I am trying to create/update webpages using CSS and modern programming (getting away from using tables to place text and objects). I am running into an issue using ul's. I will give you...
13
by: clintonG | last post by:
// I want this format... <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/"> The WriteStartElement is used for the rss element and...
2
by: penaloj | last post by:
I am assembling a database of scientific names, e.g. Acer saccharum Marshall, sugar maple. The first two elements (genus and species) should be italicized in the report, the third element (author)...
6
by: Tomasz J | last post by:
Hello developers, I bind my TextBox control specyfying a format stored in my application global ApplicationContext object - it has a static string CurrencyFormat property. The problem - this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.