Convert string --> XML

G

Gab

Hello,

I 've got a little problem but i don't know how to resolv it .

I use a WebService , this webservice return me à string like this
<?xml version="1.0" encoding="UTF-8"?><RESULT><GRP ID="BPC0_1"><FLD
NAME="BPCNUM" TYPE="Char">000GM3</FLD><FLD NAME="BPCNAM"
TYPE="Char">GAMES-UPD5</FLD></GRP><GRP ID="BPRC_1"><FLD NAME="BPRSHO"
TYPE="Char">GAMES-UPD5</FLD><FLD NAME="BPRLOG" TYPE="Char"/><LST
NAME="BPRNAM" SIZE="1" TYPE="Char"><ITM>GAMES-UPD5</ITM></LST><FLD
NAME="ZSAGE" TYPE="Char"/>


this string is XML code but I would like to convert this string into XML
format like this
<?xml version="1.0" encoding="UTF-8"?>

<RESULT>

<GRP ID="BPC0_1">

<FLD NAME="BPCNUM" TYPE="Char">000GM3</FLD>

<FLD NAME="BPCNAM" TYPE="Char">GAMES-UPD5</FLD>

</GRP>

<GRP ID="BPRC_1">

<FLD NAME="BPRSHO" TYPE="Char">GAMES-UPD5</FLD>

<FLD NAME="BPRLOG" TYPE="Char"/>

<LST NAME="BPRNAM" SIZE="1" TYPE="Char">

<ITM>GAMES-UPD5</ITM>

</LST>

<FLD NAME="ZSAGE" TYPE="Char"/>


I didn't find how to do .

Thanks for your help.
Gab
 
M

Martin Honnen

Gab said:
I use a WebService , this webservice return me à string like this
<?xml version="1.0" encoding="UTF-8"?><RESULT><GRP ID="BPC0_1"><FLD
NAME="BPCNUM" TYPE="Char">000GM3</FLD><FLD NAME="BPCNAM"
TYPE="Char">GAMES-UPD5</FLD></GRP><GRP ID="BPRC_1"><FLD NAME="BPRSHO"
TYPE="Char">GAMES-UPD5</FLD><FLD NAME="BPRLOG" TYPE="Char"/><LST
NAME="BPRNAM" SIZE="1" TYPE="Char"><ITM>GAMES-UPD5</ITM></LST><FLD
NAME="ZSAGE" TYPE="Char"/>

Assuming you have a string of well-formed XML (which the above is not)
you can do e.g.
XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
string xml;
using (StringWriter sw = new StringWriter())
{
doc.Save(sw);
xml = sw.ToString();
}
and then xml contains line breaks and indentation. You will however not
get empty lines between nodes, as you seem to want, based on your
description below:
this string is XML code but I would like to convert this string into XML
format like this
<?xml version="1.0" encoding="UTF-8"?>

<RESULT>

<GRP ID="BPC0_1">


If you really want empty lines between nodes and no indentation of child
nodes then you can use

string yourString = "<root><foo>1</foo></root>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
string xml;

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "";
settings.NewLineChars = "\r\n\r\n";

using (StringWriter sw = new StringWriter())
{
using (XmlWriter xw = XmlWriter.Create(sw, settings))
{
doc.Save(xw);
}
xml = sw.ToString();
}
Console.WriteLine(xml);
 
G

Gab

Perfect !

Thanks for your help .

Gab

Martin Honnen said:
Assuming you have a string of well-formed XML (which the above is not) you
can do e.g.
XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
string xml;
using (StringWriter sw = new StringWriter())
{
doc.Save(sw);
xml = sw.ToString();
}
and then xml contains line breaks and indentation. You will however not
get empty lines between nodes, as you seem to want, based on your
description below:



If you really want empty lines between nodes and no indentation of child
nodes then you can use

string yourString = "<root><foo>1</foo></root>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
string xml;

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "";
settings.NewLineChars = "\r\n\r\n";

using (StringWriter sw = new StringWriter())
{
using (XmlWriter xw = XmlWriter.Create(sw, settings))
{
doc.Save(xw);
}
xml = sw.ToString();
}
Console.WriteLine(xml);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top