Can anyone tell me why my XML writer is not writing attributes?

Posted by user1632018 on Stack Overflow See other posts from Stack Overflow or by user1632018
Published on 2013-10-30T03:30:18Z Indexed on 2013/10/30 3:54 UTC
Read the original article Hit count: 210

Filed under:
|
|
|
|

I am writing a parsing tool to help me clean up a large VC++ project before I make .net bindings for it.

I am using an XML writer to read an xml file and write out each element to a new file. 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;

                                }
                        }

                    }

                }

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET