Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have the following XML lines:

HTML
<a>
<parm name="InstallPhase" value="install" />
</a>


but when I used the code below, I got the middle line (element with attributes but not value) as:

HTML
<a>
        <parm name="InstallPhase" value="install">
        </parm>
</a>


How it could be prevented? here is my code
C#
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.NewLineOnAttributes = true;
            xmlWriterSettings.Indent = true;
            XmlTextReader reader = new XmlTextReader(@"c:\c.xml");
            XmlTextWriter writer = new XmlTextWriter(@"c:\b.xml", null);
            writer.Formatting = Formatting.Indented;
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        Console.Write("<" + reader.Name);
                        writer.WriteStartElement(reader.Name);
                        //writer.
                        Console.WriteLine(">");
                        if (reader.HasAttributes)
                        {
                            Console.WriteLine(reader.Name + " ------------------Attribute");
                            for (int i = 0; i < reader.AttributeCount; i++)
                            {
                                reader.MoveToAttribute(i);
                                Console.WriteLine("Nam: " + reader.Name + ", Value: " + reader.Value);
                                writer.WriteAttributeString(reader.Name, reader.Value);
                            }
                            reader.MoveToElement();
                        }
                        //writer.WriteElementString(reader.Name, null);
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        Console.WriteLine(reader.Value);
                        //writer.WriteString(reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        Console.Write("                       Console.WriteLine(">");
                        writer.WriteFullEndElement();
                        break;
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.ProcessingInstruction:
                        writer.WriteProcessingInstruction(reader.Name, reader.Value);
                        break;
                    case XmlNodeType.Comment:
                        writer.WriteComment(reader.Value);
                        break;
                        
                }
            }
            reader.Close();
            writer.Close();
Posted
Updated 13-Nov-11 19:56pm
v2
Comments
Pandya Anil 14-Nov-11 1:49am    
I dont see any difference in both the lines... both are same ...

We dont understand your question.. I guess...

1) <parm name="InstallPhase" value="install" />

2) <parm name="InstallPhase" value="install" > </parm>
Sergey Alexandrovich Kryukov 14-Nov-11 1:52am    
That's right: there is no difference between code (1) and (2). Do you know physical and logical structure of XML?
--SA
MotiP 14-Nov-11 2:00am    
They are the same, but I would like to copy it as is to have the first line ( <parm name="InstallPhase" value="install" />) and not the second line, while the code give me the second line (<parm name="InstallPhase" value="install" > </parm>).

1 solution

There is no difference between the lines. The first uses an empty element tag while the second uses an end tag. The only difference is in the look, they are both equivilent and behave the same.

A much easier way to transform one document into another is to use XSLT
 
Share this answer
 
Comments
MotiP 14-Nov-11 3:35am    
I'm not familiar with the XSLT and if it will be good enough to me to make additional functionality like change some strings in the attributes and element values. Therefore seems to me better to continue with the XMLTextWritter class.

Thanks
Moti
[no name] 14-Nov-11 4:11am    
Seems to me the time to learn something new and not use a hammer for everything

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900