0

谁能告诉我为什么我的 XML 编写器在他们这样吃的时候没有写出结束标签:/ >

我正在做的是读取一个 xml 文件,然后将其写入一个新文件,如果找到某些元素,我会执行一些代码,然后用新的元素覆盖这些元素。整个目的是复制第一个文档中几乎所有的 xml,除非它找到需要执行不同代码的特定元素,这些代码将依次输出作者添加的新元素。到目前为止,它几乎可以正常工作,除了结束标签。

这是它应该是什么样子的一个片段(用星号标注的不同示例):

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <ProjectGuid>{57900E99-A405-49F4-83B2-0254117D041B}</ProjectGuid>
    <Keyword>Win32Proj</Keyword>
    <RootNamespace>libprojex</RootNamespace>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" **/>**
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    <ConfigurationType>DynamicLibrary</ConfigurationType>
    <UseDebugLibraries>true</UseDebugLibraries>
    <CharacterSet>MultiByte</CharacterSet>
    **<PlatformToolset>v110</PlatformToolset>**
  </PropertyGroup>

这是我的输出:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <ProjectGuid>{57900E99-A405-49F4-83B2-0254117D041B}</ProjectGuid>
    <Keyword>Win32Proj</Keyword>
    <RootNamespace>libprojex</RootNamespace>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props">
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
      <ConfigurationType>DynamicLibrary</ConfigurationType>
      <UseDebugLibraries>true</UseDebugLibraries>
      <CharacterSet>MultiByte</CharacterSet>
    </PropertyGroup>

这些只是简短的摘录。他们在整个文档中继续。如果您注意到以 结尾的行/>,它不会正确地将它们添加到编写器。输出中也缺少此行<PlatformToolset>v110</PlatformToolset>

我已经包含了一些代码来展示我是如何实现这一点的:

string vcName = Path.GetFileName(textBox1.Text);
string vcProj = Path.Combine(baseDir, vcName);

using (XmlReader reader = XmlReader.Create(textBox1.Text))
{
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.ConformanceLevel = ConformanceLevel.Auto;
    settings.Indent = true;
    settings.CloseOutput = false;
    string nameSpace = "http://schemas.microsoft.com/developer/msbuild/2003";
    using (XmlWriter writer = XmlWriter.Create(vcProj, settings))
    {

        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:

                   if (reader.Name == "ClInclude")
                    {
                        //execute code here- omitted for example
                        writer.WriteStartElement(reader.Name, nameSpace);
                        writer.WriteAttributeString("Include", "include/" + filename);
                        writer.WriteEndElement();

                    }
                    else if (reader.Name == "ClCompile" && reader.HasAttributes)
                    {
                        //execute code here- omitted for example
                        writer.WriteStartElement(reader.Name, nameSpace);
                        writer.WriteAttributeString("Include", "src/" + filename);
                        writer.WriteEndElement();

                    } 
                   else
                    {
                        writer.WriteStartElement(reader.Name, nameSpace);
                        writer.WriteAttributes(reader, true);
                    }

                    break;

                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    break;
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    break;
                case XmlNodeType.Comment:
                    writer.WriteComment(reader.Value);
                    break;
                case XmlNodeType.Attribute:
                    writer.WriteAttributes(reader, true);
                    break;
                case XmlNodeType.EntityReference:
                    writer.WriteEntityRef(reader.Value);
                    break;
               case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;

                }
        }

    }

谁能告诉我为什么我会遇到这些问题,是它以某种方式忽略了无效的 XML 吗?

4

2 回答 2

0

似乎 Import 既是 Element 又是 EndElement。但是在 Element 中创建它之后,您只需突破而不关闭。

PS 使用 LinqToXML 和 XElement 会容易得多...

于 2013-10-30T17:27:43.843 回答
0

我怀疑您的代码的这一部分:

else
{
  writer.WriteStartElement(reader.Name, nameSpace);
  writer.WriteAttributes(reader, true);
}

尝试添加 writer.WriteEndElement(); 就像你对其他 if 块所做的那样。

于 2013-10-30T17:27:58.017 回答