Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to generate an XML file from a datagridview that looks like this:
A0001,Horse on Wheels,5,No
A0002,Elephant on Wheels,2,No
A0003,Dog on Wheels,0,Yes
A0004,Seal on Wheels,3,No


It has 4 elements i.e an Order Number, Item description, Current count and On order.
When the XML file is generated however, only the top row is used. How can I make it so that a different row is used for different items?

What I have tried:

my code is as follows
C#
string source = Clipboard.GetText(TextDataFormat.CommaSeparatedValue);
XElement cust = new XElement("Root",
    from str in source
    let fields = source.Split(',', '\n')
    select new XElement("Item",
        new XElement("OrderId", fields[0]),
        new XElement("ItemDescription", fields[1]),
        new XElement("CurrentCount", fields[2]),
        new XElement("OnOrder", fields[3])
    )
);


I have a button that copies all the data in the grid to the clipboard, code is as follows:

C#
// Save the current state of the clipboard so we can restore it after we are done
           IDataObject objectSave = Clipboard.GetDataObject();

           // Choose whether to write header
           dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
           // Selects all the cells in order to copy them
           dataGridView1.SelectAll();
           // Copy (this sets the clipboard)
           Clipboard.SetDataObject(dataGridView1.GetClipboardContent());


Any help would be greatly appreciated. I am very new to coding and have been trying to teach myself for only a few weeks.
Posted
Updated 25-May-16 20:06pm
v4
Comments
Sergey Alexandrovich Kryukov 26-May-16 0:03am    
This is not a matter of "how"; it's a matter of "why". Indeed, if you can correctly parse some text file into XML, why the input file is not XML? Now, you don't need XML by itself, you need it, because it's easily parsed to data structures. But let's assume you cannot have XML, it has to be a plain text of some different structure, just because it is some 3rd-party format. But then, why not parsing it immediately to data?

You can get any number of ways of doing what it want, but the solutions like that would not eliminate certain absurdity of the ill-posed problem. If I missed something, please explain.

—SA
Member 12546783 26-May-16 0:59am    
No it is not a matter of why. This is simply me learning to do something new and is never intended to be used in a real life situation. All I wanted was someone to help explain how I could parse the different rows in, instead you just come and belittle someone who obviously is very new to this.
Sergey Alexandrovich Kryukov 26-May-16 10:40am    
This is a respectful purpose, sure. But it's always good to start think at reasonable design from the very beginning.
—SA

1 solution

try this

C#
string pathofTextFile = @"D:\text.txt"; 

        string[] source = File.ReadAllLines(pathofTextFile);
        XElement cust = new XElement("Root",
            from str in source
            let fields = str.Split(',')
            select new XElement("Item",
                new XElement("OrderId", fields[0]),
                new XElement("ItemDescription", fields[1]),
                new XElement("CurrentCount", fields[2]),
                new XElement("OnOrder", fields[3])
            )
        );
 
Share this answer
 
Comments
Member 12546783 26-May-16 0:18am    
Thanks for the reply! I need to use the clipboard however as sometimes the text file needs to be edited in a gridview before being exported to XML.
Karthik_Mahalingam 26-May-16 0:30am    
so you want to copy the text from gridivew ?
But you have mentioned as "I am trying to generate an XML file from a text file "
So I gave the answer in this manner
Member 12546783 26-May-16 1:00am    
yes I see my question is a bit off now. Your reply is spot on for the question asked. It has been updated now, thanks for bringing this to my attention
Karthik_Mahalingam 26-May-16 1:02am    
gridview(asp) or datagridview(windows)
Member 12546783 26-May-16 1:03am    
gridview(asp)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900