Intro: Hi fellow coders; I created a project to create XML files to automate filecreation with certain settings.
Issue: my knowledge isn't sufficient to recreate this. I can't find the right information on two questions;
A. How to create an element inside an element (using a second writestartelement closes the first one);
B. On line 3 there's an element which I do not recognise. Can anybody tell me which type I should be looking for on the net?
<userEnvironmentSettings>
<conditions>
<ref n="Printer - PR123456.xml" />
</conditions>
</userEnvironmentSettings>
Current code (Will keep this up to date)
writer.WriteStartElement("userEnvironmentSettings");
writer.WriteStartElement("conditions");
writer.WriteAttributeString("ref n", "Printer - " + printerName + ".xml\"");
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
Result: $exception as per a whitespace cannot be set inside Attributestring.
You need an element called
refwith an attribute calledn. What you've tried to do is create an attribute calledref nwithin theconditionselement, which is not allowed (as it contains whitespace) and is not what you want. So this would work:However, unless you have good reason I'd strongly suggest you don't use
XmlWriterdirectly. There are much easier, higher-level APIs you can use. For example: