如何在xml文件中写点符号?(How to write dot notation in xml file?)

编程入门 行业动态 更新时间:2024-10-21 23:19:15
如何在xml文件中写点符号?(How to write dot notation in xml file?)

我正在使用XmlWriter在C#中编写XML文件。 我可以使用WriteStartElement()方法编写Elements,并且可以使用WriteAttributeString()方法向此元素添加属性。 但是,如果我想使用点表示法添加属性,那么该怎么做呢?

<Element Attribute1="Value"> <Element.Attribute2> //How can i add attribute in this Notation. //Add Value of Attribute2 </Element.Attribute2> </Element>

我知道我可以调用WriteStartElement("Element.Attribute")但我正在寻找一种更清洁的方法。 有没有其他方法可以做到这一点?

EDIT1:

我有一个分层的对象(比如obj)(以树的形式),这个树的每个节点都有一些属性,可以进一步包含节点。 我将此对象保存在Xml中。 为此我使用XmlWriter。 在运行时,我遍历obj并使用GetType()读取节点类型.Name并传递它以编写XmlNode并使用GetType().GetProperties()我得到该节点的所有属性,然后我使用foreach去通过PropertyInfo数组一个接一个地将PropertyInfo的Name写为属性,但是如果我有一个分配了节点的属性,我需要为此编写上面的Dot Notation。 我正在寻找一种方法,我将只传递我的PropertyInfo和对象,它将以所需的格式为我写。

谢谢你的帮助!

EDIT2:

对于特定节点,我有像Height和Width这样的属性,比如Children是一个Collection,它隐含在Xml的层次结构中,和Resources一样,它们也有一些属性,每个属性都由父节点下的节点表示。 但保存时会写成:

<Parent.Resources> <Resource1 ...../> <Resource2 ...../> </Parent.Resources>

感谢帮助!

I am writing an XML file in C# using XmlWriter. I am able to write Elements using WriteStartElement() method and I can add attributes to this element using WriteAttributeString() method. But if I want to add attribute using dot notation then how to do that?

<Element Attribute1="Value"> <Element.Attribute2> //How can i add attribute in this Notation. //Add Value of Attribute2 </Element.Attribute2> </Element>

I know that I can call WriteStartElement("Element.Attribute") but I m looking for a cleaner approach. Is there any other way to do this?

Edit1:

I have an object(say obj) that is hierarchical(in the form of Tree), each node of this tree is having some properties which can further contain nodes. and I am saving this object in Xml. For that I am using XmlWriter. At runtime I iterate through the obj and read the type of node using GetType().Name and pass it to write an XmlNode and using GetType().GetProperties() I get all properties of that node, then I use a foreach to go through the PropertyInfo array one by one and write the Name of PropertyInfo as attribute but in case when I am having a property that is assigned a node, I need to write the above Dot Notation for that. I am looking for a method where I will just pass my PropertyInfo and the object and it will write for me in desired format.

Thanks for any help!

Edit2:

For a particular node I have properties like Height and Width, like Children which is a Collection and resides implicitly in the hierarchy of Xml, and like Resources which will also be having some properties and each will be represented by nodes under the parent. But while saving will be written like:

<Parent.Resources> <Resource1 ...../> <Resource2 ...../> </Parent.Resources>

Thanks for help!

最满意答案

什么比WriteStartElement("Element.Attribute")更干净? 它准确描述了您正在做的事情 - 创建一个具有该名称的新元素。

如果你肯定想使用XmlWriter ,我会坚持使用这种方法。 然而,正如Henrik所说,LINQ to XML通常是一种更简单的创建XML的方法:

XElement element = new XElement("Element", new XAttribute("Attribute1", "Value"), // This could contain nested elements instead of just a text node new XElement("Element.Attribute2", "Second value") );

编辑:现在你已经更新了问题,我仍然不明白你为什么要使用这个“点符号”。 它不是隐含在XML的层次结构中吗?

What would be cleaner than WriteStartElement("Element.Attribute")? It describes exactly what you're doing - creating a new element, with that name.

If you definitely want to use XmlWriter, I'd stick with that approach. As Henrik says, however, LINQ to XML is generally a simpler way of creating XML in the first place:

XElement element = new XElement("Element", new XAttribute("Attribute1", "Value"), // This could contain nested elements instead of just a text node new XElement("Element.Attribute2", "Second value") );

EDIT: Now you've updated the question, I still don't see why you want to use this "dot notation". Isn't it implicit in the hierarchy of the XML?

更多推荐

本文发布于:2023-07-26 15:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277516.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:写点   符号   文件   如何在   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!