具有属性但没有值格式的C#XML编写元素 [英] c# xml writing element with attribute but without value format

查看:91
本文介绍了具有属性但没有值格式的C#XML编写元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下XML行:

Hi,

I have the following XML lines:

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

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



如何预防?这是我的代码



How it could be prevented? here is my code

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();

推荐答案

行之间没有区别.第一个使用空元素标签,第二个使用结束标签.唯一的区别在于外观,它们既相等又表现相同.

将一个文档转换为另一个文档更简单的方法是使用XSLT
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


这篇关于具有属性但没有值格式的C#XML编写元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆