1

I just started using VS2010 and C#.

I'm trying to create an app which takes values from two textboxes and adds it to an existing xml file.

eg.

Text Box 1       Text Box 2
----------       ----------
A                B
C                D
E                F

I want the resultant xml file to be like this.

<root>
 <element>
 <Entry1>A</Entry1>
 <Entry2>B</Entry2>
 </element>
</root>

and so on...

Can this be done using C# ??

I'm unable to add the entries alternatively i.e. Entry1 should contain Text Box 1 line #1 and Entry2 Text Box 2 line #1.

Any help would be appreciated.

Thanks

John Saunders
  • 160,644
  • 26
  • 247
  • 397
vs2010noob
  • 213
  • 1
  • 6
  • 13

2 Answers2

1

You need to split the string retrieved from the text box based on the new line like this:

string[] lines = theText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

Once you have values split for each text box, you can use System.xml.linq.xdocument class and loop through the values that you retrieve above.

Something like this:

XDocument srcTree = new XDocument(new XElement("Root",
    new XElement("entry1", "textbox value1")))

You can retrieve a xml document using a linq query or save it in an xml file using the Save method of XDocument

The below code will give you a string of XML data from the textboxes:

private string createXmlTags(TextBox textBox1, TextBox textBox2)
    {
        string strXml = string.Empty;
        string[] text1Val = textBox1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        string[] text2Val = textBox2.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        int count = 1;
        IList<XElement> testt = new List<XElement>();
        
        for (int i = 0; i < text1Val.Count(); i++)
        {
            testt.Add(new XElement("Entry" + count, text1Val[i]));
            while (!String.IsNullOrEmpty(text2Val[i]))
            {
                count = count + 1;
                testt.Add(new XElement("Entry"+count,text2Val[i]));
                break;
            }
            count = count + 1;
        }
        foreach (var xElement in testt)
        {
            strXml += xElement.ToString();
        }
        return strXml;
    }

You can then insert the code to an existing xml document. Follow: How can I build XML in C#? and How to change XML Attribute

Community
  • 1
  • 1
reggie
  • 13,313
  • 13
  • 41
  • 57
  • Answer has been updated with a code to to create xml tags. I have also provided links to update an exisitng xml file with the string retrieved from the method – reggie Aug 12 '11 at 15:24
  • What is the difference between this code and previous one which you entered ? Wouldn't that work or is this easier ? – vs2010noob Aug 12 '11 at 16:13
  • I tested that, but it didnt work. This is easier and works for sure – reggie Aug 12 '11 at 16:29
  • Does exactly what I wanted...I have changed the code to suit myself and will be using the same for further projects :) Is using strings the easy way to add data such as this in an XML file ? – vs2010noob Aug 14 '11 at 12:32
  • If you are using strings, I believe that is the easiest way to separate them. I am glad that I could help :) – reggie Aug 15 '11 at 11:55
0

Read here: XDocument or XmlDocument

I will have the decency of not copying the code from there. Every basics you need to know on creating a XML doc is well explained there.

There are two options, I would personally go with XDocument.

I know there's no code in this answer but since you haven't tried anything, not even apparently searching Google (believe me, you'd find it), I'd rather point you in the right direction than "giving you the fish".

Community
  • 1
  • 1
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • Trust me...I've been searching but don't really know what to search for :p...there are so many ways XML files can be manipulated I was lost. Is XDocument the best way for editing XML's ? – vs2010noob Aug 12 '11 at 13:59