0

Want to convert CSV to XML, using this code:

            // test person
        var person = new Person("Jim", "TestJob", 1000);
        var fileStream = new FileStream("sample.xml", FileMode.Create);
        var writer = XmlDictionaryWriter.CreateTextWriter(fileStream);
        var dcs = new DataContractSerializer(typeof(Person));

        // Use the writer to start a document.
        writer.WriteStartDocument(true);

        // Use the writer to write the root element.
        writer.WriteStartElement("Company");

        // Use the writer to write an element.
        writer.WriteElementString("Name", "Microsoft");

        // Use the serializer to write the start,
        // content, and end data.
        dcs.WriteObject(writer, person);

        // Use the writer to write the end element and
        // the end of the document
        writer.WriteEndElement();
        writer.WriteEndDocument();

        // Close and release the writer resources.
        writer.Flush();
        fileStream.Flush();
        fileStream.Close();

Problem I have, no tabs, why is that? enter image description here

After adding some tabs, the code that is generated seems to be right too! enter image description here Should I ignore it? Can this be done? Already searched around but not much to found about the tabs themselves.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Jim Vercoelen
  • 1,048
  • 2
  • 14
  • 40
  • Is there a reason you are using `XmlDictionaryWriter` and not `XmlWriter`? `XmlWriter.Create` allows you to pass in settings that have an Indent property, which you would set to true to force indenting. – Mike Zboray Oct 16 '15 at 17:08

1 Answers1

0

The XmlDictionaryWriter has a Settings property, and one of those setting properties is Indent.

https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings(v=vs.110).aspx

Paul Abbott
  • 7,065
  • 3
  • 27
  • 45
  • Like this: writer.Settings.Indent = true; ? Because this gives object not set to a reference error, which is weird because I added this line after: "var dcs = new DataCon..." – Jim Vercoelen Oct 16 '15 at 17:01
  • `XmlDictionaryWriter` gives you no way to set the `Settings` property and it is not overridden in the source code so it defaults to null. You would have to subclass `XmlDictionaryWriter` to override the settings. – Mike Zboray Oct 16 '15 at 17:10
  • 1
    Look at the sample code at the bottom of the page -- you need to create a new `XmlWriterSettings()` and pass it into `Create`. To do this, you need to use `XmlWriter` instead http://stackoverflow.com/questions/3604123/xmlwriter-vs-xmldictionarywriter – Paul Abbott Oct 16 '15 at 17:12
  • I figures it out with your guidance, so thanks for that one Paul! Would like to upvote your answer but it is not fully correctly in my case, so for that – Jim Vercoelen Oct 16 '15 at 21:04