What are the different ways of writing xml values in C#

I am new to .xml files in general and I am currently working on project where I take simple .txt file with values inside of it and generating a .xml file out of it.

I am trying to replicate this in the xml file:
<value1 name="value2"/>
Only value2 gets changed based on the .txt file.

Using

xmlWriter.WriteStartElement() 

with

xmlWriter.WriteElementString() 

I am getting working files but the resulting .xml file looks different from what I want.

<value1 name="value2"> </value1>

>Solution :

Try this:

xmlWriter.writeStartElement("value1");
xmlWriter.writeAttributeString("name", "value2");
xmlWriter.writeEndElement();

also set:

XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Indent = true;
wSettings.ConformanceLevel = ConformanceLevel.Fragment;
wSettings.OmitXmlDeclaration = true;

Leave a Reply