2013-10-30 39 views
0

我正在写一个解析工具来帮助我清理一个大的VC++项目,然后再为它制作.net绑定。谁能告诉我为什么我的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>libproj</RootNamespace> 
    </PropertyGroup> 

这是我得到的输出(无属性):

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

这是我的代码目前。我已经尽了各种方法来写出属性。

   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; 

           } 
         } 

        } 

       } 
+1

我几乎可以肯定你的问题是你的XML命名空间的无知和\或“ConformanceLevel.Fragment” – Soonts

+0

对不起,我忘了提,我加ConformanceLevel.Fragment后我收到一个错误,指出由于XML无效,我需要添加它。我在尝试不同的选项时实际上已将其删除,但创建复制品时我无意中再次添加了它。就输出而言,它是无关紧要的。尽管请解释为什么我在这件事上的无知是问题所在? – user1632018

+0

@Soonts你基本上是对的。我的问题是我对XML命名空间的无知,并且缺乏它。我也将我的一致性级别更改为Auto。谢谢。 – user1632018

回答

1

我结束了看起来有点矿石到Soonts评论后命名空间,并实现了为什么我的尝试之一是行不通的。我必须事先指定名称空间,而不是让作者将其复制到阅读器XML文件中。这是我如何解决我的问题:

     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; 

             } 
           } 

          } 

         } 
相关问题