0

I am trying to format an XML page in C# per the request of client. Below is what I currently create in XML

<?xml version="1.0" encoding="utf-8"?>
<!--This is to write the connection strings, text file location, and report destination.-->
<AdminPaths Name="sqlConnection1" connectionString="asdf">
  <TextPath>
    <Key Value="Test3" xmlns="Path" />
  </TextPath>
</AdminPaths>

This is what I would like to have:

<?xml version="1.0" encoding="utf-8"?>
<!--This is to write the connection string, text file location, and report destination.-->
<AdminPaths> 
  <Name="sqlConnection1" connectionString="asdf">
</AdminPaths>
<TextPath>
  < Key="Path" Value="Test3">
  < Key="Report" Value="Test2">
</TextPath>

Here is the code I am currently using:

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        XmlWriter writer = XmlWriter.Create("C:\\Users\\fthompson11\\WebFile.xml", settings);
        writer.WriteStartDocument();
        writer.WriteComment("This is to write the connection strings, text file location, and report destination.");
        writer.WriteStartElement("AdminPaths");
        writer.WriteAttributeString("Name", "sqlConnection1");
        writer.WriteAttributeString("connectionString", "asdf");
        writer.WriteStartElement("TextPath");
        writer.WriteStartElement("Key", "Path");
        writer.WriteAttributeString("Value", "Test3");
        writer.WriteEndElement();
        writer.WriteEndDocument();

        writer.Flush();
        writer.Close();

I have tried using writer.WriteEndElement(); after the writeAttributeString to start but I receive an error that states "Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment." I believe that is not what I want to do? Any help would be appreciated.

thanks.

12
  • 5
    "What I would like to have" looks like invalid XML... Commented Apr 10, 2013 at 22:28
  • What is incorrect about it? Commented Apr 10, 2013 at 22:29
  • @BigPoppa xml should have single root element Commented Apr 10, 2013 at 22:30
  • @lazyberezovsky by single root element, do you mean i need to only have <adminpaths>? Commented Apr 10, 2013 at 22:31
  • 1
    The <Key=""/> wont work. You're describing a nameless element with 2 attributes. Commented Apr 10, 2013 at 22:32

2 Answers 2

2

I think this is the XML that you want. I'm guessing, because the XML you state as your goal is not correctly formed.

<?xml version="1.0" encoding="utf-8"?>
<!--This is to write the connection strings, text file location, and report destination.-->
<AdminPaths>
  <AdminPath Name="sqlConnection1" connectionString="asdf" />
  <TextPath>
    <Text Key="Path" Value="Test3" />
    <Text Key="Report" Value="Test2" />
  </TextPath>
</AdminPaths>

Basically, in your stated goal XML, you were trying to create 2 root nodes under the xml document, which is a no-no, unless you are willing to use ConformanceLevel.Fragment or ConformanceLevel.Auto and force this behavior.

The code for this would like the following:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

XmlWriter writer = XmlWriter.Create("C:\\Users\\fthompson11\\WebFile.xml",
  settings);
writer.WriteStartDocument();
writer.WriteComment("This is to write the connection strings, text file location, and report destination.");

// the AdminPaths
writer.WriteStartElement("AdminPaths");
writer.WriteStartElement("AdminPath");
writer.WriteAttributeString("Name", "sqlConnection1");
writer.WriteAttributeString("connectionString", "asdf");
writer.WriteEndElement();

// the TextPath's
writer.WriteStartElement("TextPath");
writer.WriteStartElement("Text");
writer.WriteAttributeString("Key", "Path");
writer.WriteAttributeString("Value", "Test3");
writer.WriteEndElement();

writer.WriteStartElement("Text");
writer.WriteAttributeString("Key", "Report");
writer.WriteAttributeString("Value", "Test2");
writer.WriteEndElement();

writer.WriteEndElement(); // </AdminPaths>
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Sign up to request clarification or add additional context in comments.

Comments

1

This is invalid XML for reasons like:

  • More than one root element, that is a single <> element around all of the stuff. In the first example, <AdminPaths> is the single root element because everything else is contained within it
  • Nodes that don't have no name. For example, <add="" is invalid - if the node's name is add then it can't have a value directly. Ways to do that is <add>Value</add>. Otherwise, you must have an attribute like <add key="">
  • Similarly, < Key="Path" Value="Test3"> is invalid because it doesn't have a name, just two attributes (Key and Value) and also it's not closed (Nodes need to either have a closing tag like <add></add> or be self-closing like <add /> (Notice the / before the >)

Read up on the difference between a Node or Element and an Attribute in XML and it should become relatively clear what you need to build, then we can address the how.

5 Comments

I think this should be a comment, not a solution
@lazyberezovsky It's too long to be a comment :/
Agree, but technically that's still a comment :)
I think it's a valid post, if only because @MichaelStum is pointing out which parts of the code need working on.
Agreed. I do not have enough rep to vote up or I would have. Thanks @MichaelStum

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.