Create simple XML file

S

shapper

string strFile = "............";
using (StreamWriter sw = File.CreateText(strFile))
{
    sw.WriteLine(@"<?xml version="1.0" encoding="utf-8" ?>");
    sw.WriteLine(@"<ArrayOfXmlProfile
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">");
    sw.WriteLine("</ArrayOfXmlProfile>");

}

Are you sure that the following can be used:
@"<?xml version="1.0" encoding="utf-8" ?>"

I get an error on the second ".

I am able to make this work only with
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"

Or
@"<?xml version='1.0' encoding='utf-8' ?>"

But in this last case I am not sure if I should replace " by '

Thanks,
Miguel
 
A

Arne Vajhøj

shapper said:
Are you sure that the following can be used:
@"<?xml version="1.0" encoding="utf-8" ?>"

I get an error on the second ".

Because it is a syntax error.
I am able to make this work only with
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"

Or
@"<?xml version='1.0' encoding='utf-8' ?>"

But in this last case I am not sure if I should replace " by '

Bot those are valid.

So should the following be:

@"<?xml version=""1.0"" encoding=""utf-8"" ?>"

Arne
 
A

Arne Vajhøj

shapper said:
I need to create a XML that does not exist just with the following:
<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfXmlProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</ArrayOfXmlProfile>

What is the best way to do this?

I think XmlTextWriter is a lot better than a simple
StringWriter because it is XML aware.

Example:

XmlTextWriter xtw = new XmlTextWriter(@"C:\empty.xml",
Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
xtw.WriteStartElement("ArrayOfXmlProfile");
xtw.WriteAttributeString("xmlns", "xsi", null,
"http://www.w3.org/2001/XMLSchema-instance");
xtw.WriteAttributeString("xmlns", "xsd", null,
"http://www.w3.org/2001/XMLSchema");
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Close();

Arne
 
J

Jeff Johnson

So should the following be:

@"<?xml version=""1.0"" encoding=""utf-8"" ?>"

Really? I didn't know you could use doubling to escape a quote. It feels so
VB-ish. (Not that that's a bad thing, just surprising.)
 

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