[英]Can anyone tell me why my XML writer is not writing attributes?
I am writing a parsing tool to help me clean up a large VC++ project before I make .net bindings for it. 我正在编写一个解析工具来帮助我清理大型VC ++项目,然后再为其进行.net绑定。
I am using an XML writer to read an xml file and write out each element to a new file. 我正在使用XML编写器来读取xml文件并将每个元素写到新文件中。 If an element with a certain name is found, then it executes some code and writes an output value into the elements value. 如果找到具有特定名称的元素,那么它将执行一些代码并将输出值写入元素值。
So far it is almost working, except for one thing: It is not copying the attributes. 到目前为止,它几乎可以正常工作,除了一件事:它没有复制属性。 Can anyone tell me why this is happening? 谁能告诉我为什么会这样吗?
Here is a sample of what it is supposed to copy/modify(Includes the attributes): 这是应该复制/修改的示例(包括属性):
<?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>libproj</RootNamespace>
</PropertyGroup>
Here is the output I am getting(No Attributes): 这是我得到的输出(无属性):
<?xml version="1.0" encoding="utf-8"?>
<Project>
<ItemGroup>
<ProjectConfiguration>
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration>
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup>
<ProjectGuid>{57900E99-A405-49F4-83B2-0254117D041B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>libproj</RootNamespace>
Here is my code currently. 这是我当前的代码。 I have tried every way I can come up with to write the attributes. 我尝试了各种方式来编写属性。
string baseDir = (textBox2.Text + "\\" + safeFileName);
string vcName = Path.GetFileName(textBox1.Text);
string vcProj = Path.Combine(baseDir, vcName);
using (XmlReader reader = XmlReader.Create(textBox1.Text))
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.Indent = true;
settings.CloseOutput = false;
using (XmlWriter writer = XmlWriter.Create(vcProj, settings))
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name == "ClInclude")
{
string include = reader.GetAttribute("Include");
string dirPath = Path.GetDirectoryName(textBox1.Text);
Directory.SetCurrentDirectory(dirPath);
string fullPath = Path.GetFullPath(include);
//string dirPath = Path.GetDirectoryName(fullPath);
copyFile(fullPath, 3);
string filename = Path.GetFileName(fullPath);
writer.WriteStartElement(reader.Name);
writer.WriteAttributeString("Include", "include/" + filename);
writer.WriteEndElement();
}
else if (reader.Name == "ClCompile" && reader.HasAttributes)
{
string include = reader.GetAttribute("Include");
string dirPath = Path.GetDirectoryName(textBox1.Text);
Directory.SetCurrentDirectory(dirPath);
string fullPath = Path.GetFullPath(include);
copyFile(fullPath, 2);
string filename = Path.GetFileName(fullPath);
writer.WriteStartElement(reader.Name);
writer.WriteAttributeString("Include", "src/" + filename);
writer.WriteEndElement();
}
else
{
writer.WriteStartElement(reader.Name);
}
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;
}
}
}
}
I ended up looking a bit ore into namespaces after Soonts comment, and realized why one of my attempts was not working. 在Soonts评论之后,我最终在名称空间中找了些矿石,并意识到了为什么我的一项尝试没有奏效。 I had to specify the namespace beforehand, instead of allowing the writer to copy it into the reader XML file. 我必须事先指定名称空间,而不是允许作者将其复制到读者XML文件中。 Here is how I solved my issue: 这是我解决问题的方法:
string baseDir = (textBox2.Text + "\\" + safeFileName);
string vcName = Path.GetFileName(textBox1.Text);
string vcProj = Path.Combine(baseDir, vcName);
using (XmlReader reader = XmlReader.Create(textBox1.Text))
{
XmlWriterSettings settings = new XmlWriterSettings();
//settings.OmitXmlDeclaration = true;
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")
{
string include = reader.GetAttribute("Include");
string dirPath = Path.GetDirectoryName(textBox1.Text);
Directory.SetCurrentDirectory(dirPath);
string fullPath = Path.GetFullPath(include);
//string dirPath = Path.GetDirectoryName(fullPath);
//MessageBox.Show("Path: " + dirPath + Environment.NewLine + "Filename: " + filename);
copyFile(fullPath, 3);
string filename = Path.GetFileName(fullPath);
writer.WriteStartElement(reader.Name, nameSpace);
writer.WriteAttributeString("Include", "include/" + filename);
writer.WriteEndElement();
}
else if (reader.Name == "ClCompile" && reader.HasAttributes)
{
string include = reader.GetAttribute("Include");
string dirPath = Path.GetDirectoryName(textBox1.Text);
Directory.SetCurrentDirectory(dirPath);
string fullPath = Path.GetFullPath(include);
//string dirPath = Path.GetDirectoryName(fullPath);
//MessageBox.Show("Path: " + dirPath + Environment.NewLine + "Filename: " + filename);
copyFile(fullPath, 2);
string filename = Path.GetFileName(fullPath);
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;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.