LeeC22

I am trying to dump out a collection of values from an editor I am writing. There are some elements that I need to write out, even though they are empty. Whenever I try something like...

writer.WriteElementString("variable", ""); or writer.WriteElementString("variable", string.Empty);

I get <variable /> instead of <variable></variable>

I tried a WriteStartElement("variable") followed by a WriteEndElement but that gave me...

<variable>
</variable>

Does the line separation make any difference to having it all on one line




Re: Visual C# Language How can I create an empty XmlElementString with XmlWriter

Harsimrat

Try something like this

XmlTextWriter tw = new XmlTextWriter(filename, null);

tw.Formatting = Formatting.Indented;

tw.WriteStartDocument();

tw.WriteStartElement(""); // This should make an empty element...





Re: Visual C# Language How can I create an empty XmlElementString with XmlWriter

LeeC22

Hi Harsimrat,

It doesn't need to be totally empty, the variable itself may be empty (or null) but it still needs to generate the "variable" element.

I.e.. <variable></variable>

whereas normally it would be <variable>Value</variable> if there was something in it.






Re: Visual C# Language How can I create an empty XmlElementString with XmlWriter

Harsimrat

i can see you already have that in the code and it should work

tw.WriteStartElement("variable", String.EMpty);





Re: Visual C# Language How can I create an empty XmlElementString with XmlWriter

Andrej Tozon

LeeC22 wrote:
I get <variable /> instead of <variable></variable>

Hi, just wondering what's wrong with having "<variable />". It's just the syntax, semantics is the same...

Andrej






Re: Visual C# Language How can I create an empty XmlElementString with XmlWriter

LeeC22

Andrej Tozon wrote:

LeeC22 wrote:
I get <variable /> instead of <variable></variable>

Hi, just wondering what's wrong with having "<variable />". It's just the syntax, semantics is the same...

Andrej

It needs the <variable></variable> as the data could potentially get parsed by a non-XML reader so it needs to specify data in a specific way.






Re: Visual C# Language How can I create an empty XmlElementString with XmlWriter

Andrej Tozon

Yes, unfortunately, there are still a lot of non-xml readers trying to parse xml files out there...

Anyway, if you're still struggling with this, here's what worked for me:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent =
true;
using (XmlWriter tw = XmlTextWriter.Create("c:\\test.xml", filename, settings))
{
   
tw.WriteStartDocument();
   
tw.WriteStartElement(
"document");
   
tw.WriteString(
string.Empty);
   
tw.WriteEndElement();
   
tw.WriteEndDocument();
}

Andrej






Re: Visual C# Language How can I create an empty XmlElementString with XmlWriter

LeeC22

Andrej Tozon wrote:

Yes, unfortunately, there are still a lot of non-xml readers trying to parse xml files out there...

Anyway, if you're still struggling with this, here's what worked for me:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent =
true;
using (XmlWriter tw = XmlTextWriter.Create("c:\\test.xml", filename, settings))
{
tw.WriteStartDocument();
tw.WriteStartElement(
"document");
tw.WriteString(
string.Empty);
tw.WriteEndElement();
tw.WriteEndDocument();
}

Andrej

Strangely, that piece of code works fine, when I incorporate that into my editor, I get the same <variable /> output.

The loop I have is basically this...

foreach (String mapTops in gameLevel.sMapTop)
{

if (mapTops == "")
{
writer.WriteStartElement(
"mapTop");
writer.WriteString(
string.Empty);
writer.WriteEndElement();
}
else
{
writer.WriteElementString(
"mapTop", mapTops);
}

}

mapTops is an ArrayList()

The only other difference is I don't use the XmlTextWriter.Create method but even when I used that instead of my current method, it still failed...

I might have to leave this on the backburner for now, it's becoming an unnecessary distraction. There's obviously something being written beforehand that is causing this change in output, I just don't know enough about XML to know what it is.

Thanks for the help though guys, much appreciated.






Re: Visual C# Language How can I create an empty XmlElementString with XmlWriter

Andrej Tozon

For what it's worth, this probably is about XmlTextWriter.Create... I replaced it with the new XmlTextWriter() in the above code and got <document />.

Andrej