誰能告訴我爲什麼我的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')就可以正常工作。如果您遇到問題,我建議您專門提出一個新問題,包括您嘗試過的內容以及您想要做什麼的更多問題。 –