谁能告诉我为什么我的XML作家没有写出结束标记时,他们吃的是这样的:/ >
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?
除了你面临的实际问题,你有什么特别的理由来使用'XmlReader'吗? LINQ to XML使用起来非常简单,除非你有大量的文档要避免读入内存,否则会让你的生活更轻松。 –
我没有理由,我刚开始这样做,并继续这样做。虽然它已被证明是少数。我试图使用Linq到XML,但我似乎无法遍历所有“ClInclude”元素。你能举一个例子吗? – user1632018
只要'foreach(在doc.Descendants(ns +“ClInclude”)中的var元素)'(其中'ns'是一个合适的'XNamespace')就可以正常工作。如果您遇到问题,我建议您专门提出一个新问题,包括您尝试过的内容以及您想要做什么的更多问题。 –