0

クライアントの要求ごとに C# で XML ページをフォーマットしようとしています。以下は、現在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>

これは私がしたいことです:

<?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>

現在使用しているコードは次のとおりです。

        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();

writer.WriteEndElement(); を使用してみました。writeAttributeString を開始した後、「XML フラグメントを書き込む場合は、ConformanceLevel 設定が ConformanceLevel.Fragment または ConformanceLevel.Auto に設定されていることを確認してください」というエラーが表示されます。それは私がやりたいことではないと思いますか?どんな助けでも大歓迎です。

ありがとう。

4

2 に答える 2

2

これがあなたが望むXMLだと思います。あなたの目標として述べたXMLが正しく形成されていないため、私は推測しています。

<?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>

基本的に、指定された目標 XML では、xml ドキュメントの下に 2 つのルート ノードを作成しようとしていましたが、この動作を使用ConformanceLevel.FragmentまたはConformanceLevel.Auto強制しない限り、これはノーノーです。

このためのコードは次のようになります。

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

これは、次のような理由で無効な XML です。

  • 複数のルート要素。つまり、<>すべてのものを囲む単一の要素です。最初の例で<AdminPaths>は、他のすべてがその中に含まれているため、単一のルート要素です。
  • 名前のないノード。たとえば、<add=""は無効です。ノードの名前が でaddある場合、値を直接持つことはできません。その方法は<add>Value</add>. それ以外の場合は、次のような属性が必要です<add key="">
  • 同様に、< Key="Path" Value="Test3">は名前がなく、2 つの属性 (キーと値) だけであり、閉じられていないため無効です (ノードは、のような終了タグを持つ<add></add>か、自己終了のようにする必要があります<add />(の前の / に注意してください>) 。

XML のノードまたは要素と属性の違いを読んで、何を構築する必要があるかが比較的明確になるはずです。その後、その方法に対処できます。

于 2013-04-10T22:36:05.950 に答える