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.

4

2 回答 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();
于 2013-04-10T22:50:01.230 回答
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.

于 2013-04-10T22:36:05.950 回答