I need to generate an XML file with the following structure but I do not have any experience with the XMLWriter class. Can somebody give me an idea? The source data is contained in a text file which I read with the StreamReader class. What I really need
to know is how to create the <internacional> element and also the <registro> element along with its values (clave, pais, titulo, artista and id).
<?xml version="1.0" encoding="utf-8" ?>
<internacional>
<registro clave="1" pais="Alemania" titulo="Gangnam Style" artista="Psy" id="9bZkp7q19f0" />
<registro clave="2" pais="Argentina" titulo="Don´t you worry child" artista="Swedish House Mafia" id="1y6smkh6c-0"/>
<registro clave="3" pais="Austria" titulo="Hall of fame" artista="The Script Ft. Will.I.Am" id="mk48xRzuNvA"/>
</internacional>
I appreciate your help WHIGHFIELD, this is what I needed. There is only one more question. My XML file contains characters used in other languages (ESPAÑA, NÄr, RÖd, for example). How do I support such characters?
JORGEMAL
Member
61 Points
207 Posts
Generating an XML file
Feb 04, 2013 12:36 AM|LINK
I need to generate an XML file with the following structure but I do not have any experience with the XMLWriter class. Can somebody give me an idea? The source data is contained in a text file which I read with the StreamReader class. What I really need to know is how to create the <internacional> element and also the <registro> element along with its values (clave, pais, titulo, artista and id).
<?xml version="1.0" encoding="utf-8" ?>
<internacional>
<registro clave="1" pais="Alemania" titulo="Gangnam Style" artista="Psy" id="9bZkp7q19f0" />
<registro clave="2" pais="Argentina" titulo="Don´t you worry child" artista="Swedish House Mafia" id="1y6smkh6c-0"/>
<registro clave="3" pais="Austria" titulo="Hall of fame" artista="The Script Ft. Will.I.Am" id="mk48xRzuNvA"/>
</internacional>
Respectfully,
Jorge Maldonado
whighfield
Star
11721 Points
1859 Posts
Re: Generating an XML file
Feb 04, 2013 01:05 AM|LINK
Something like this should get you started:
public string GetXmlData() { using (var writer = new StringWriter()) { using (XmlWriter xml = new XmlTextWriter(writer)) { xml.WriteStartElement("internacional"); foreach (var data in YourDataList) { xml.WriteStartElement("registro"); xml.WriteAttributeString("clave", "1"); xml.WriteAttributeString("pais", "Alemania"); xml.WriteAttributeString("titulo", "Gangnam Style"); xml.WriteAttributeString("artista", "Psy"); xml.WriteAttributeString("id", "9bZkp7q19f0"); xml.WriteEndElement(); } xml.WriteEndElement(); } return writer.ToString(); } }Blog | I need more space:DropBox Referral
kaysar98
Member
176 Points
38 Posts
Re: Generating an XML file
Feb 04, 2013 02:59 AM|LINK
Hi JORGEMAL,
actually that very much easy. first of all populate a dataset then use the code something link:
dsYourDateSet.WriteXml("YourFileName");JORGEMAL
Member
61 Points
207 Posts
Re: Generating an XML file
Feb 04, 2013 06:59 PM|LINK
I appreciate your help WHIGHFIELD, this is what I needed. There is only one more question. My XML file contains characters used in other languages (ESPAÑA, NÄr, RÖd, for example). How do I support such characters?
whighfield
Star
11721 Points
1859 Posts
Re: Generating an XML file
Feb 04, 2013 08:36 PM|LINK
You can read this and it will help http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/
Blog | I need more space:DropBox Referral
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Generating an XML file
Feb 05, 2013 11:55 PM|LINK
Hi,
You can make your xml's encoding is utf-8
JORGEMAL
Member
61 Points
207 Posts
Re: Generating an XML file
Mar 05, 2013 10:37 PM|LINK
I just found that my problem IS NOT when I generate the XML file. I will describe how I generate my XML file.
1. I receive an EXEL file by email.
2. I open the EXEL file and save it as a tab-delimited text file.
3. I StreamRead the text file and XmlTextWrite.
StreamReader srReader = new StreamReader(MapPath("Internacional.txt"), System.Text.Encoding.UTF8);
XmlTextWriter objWriter =new XmlTextWriter(MapPath("InternacionalNuevo.xml"), System.Text.Encoding.UTF8);
I ran my web form in debug mode and see that charcters like Ñ are not read correctly in step 3 with the StreamReader.
Any comments will be appreciated.
JORGEMAL
Member
61 Points
207 Posts
Re: Generating an XML file
Mar 06, 2013 12:33 AM|LINK
I found the solution, I only needed to read the text file with the correct encoding which, in my case, it is Windows-1252:
StreamReader srReader = new StreamReader(MapPath("Internacional.txt"), System.Text.Encoding.GetEncoding("Windows-1252"));