2013-10-30 75 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; 

             } 
           } 

          } 

         } 
相關問題