如何在xml文件中写入点符号?

发布于 2024-08-16 22:10:41 字数 1114 浏览 6 评论 0原文

我正在使用 XmlWriter 用 C# 编写 XML 文件。我可以使用 WriteStartElement() 方法编写元素,并且可以使用 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 的名称写入为属性,但如果我有一个分配了节点的属性,我需要为此编写上面的点表示法。我正在寻找一种方法,只需传递 PropertyInfo 和对象,它就会以所需的格式为我写入。

感谢您的帮助!

Edit2:

对于特定节点,我具有“高度”和“宽度”等属性,如“子项”,它是一个集合并隐式驻留在 Xml 的层次结构中,如“资源”,它也将具有一些属性,每个属性都由父项下的节点表示。 但保存时会写成这样:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原谅我要高飞 2024-08-23 22:10:41

还有什么比 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?

马蹄踏│碎落叶 2024-08-23 22:10:41

不存在“点符号”这样的东西。您似乎指的是 XAML。除了 XAML 之外,XML 中不存在“点表示法”之类的东西。这就是为什么你找不到任何支持它的原因——它不存在。

There is no such thing as "dot notation". You seem to be referring to XAML. Beyond XAML, there is no such thing as "dot notation" in XML. That's why you're not finding any support for it - it doesn't exist.

随波逐流 2024-08-23 22:10:41

最后,我最终以这种方式使用这个 string.Format 。在这里:

WriteXml(XmlWriter writer, Transform sender)
{
    string elementName = sender.GetType.Name;
    writer.WriteStartElement(elementName);   
    ............................
    ............................
    //and for each property inside a foreach
    writer.WriteStartElement(GetDotElement(elementName, propertyName));
}

private string GetDotElement(string elementName, string propertyName)
{
    return string.Format("{0}.{1}", elementName, propertyName);
}

谢谢你帮助我!

Finally, I end up using this string.Format in this way. Here :

WriteXml(XmlWriter writer, Transform sender)
{
    string elementName = sender.GetType.Name;
    writer.WriteStartElement(elementName);   
    ............................
    ............................
    //and for each property inside a foreach
    writer.WriteStartElement(GetDotElement(elementName, propertyName));
}

private string GetDotElement(string elementName, string propertyName)
{
    return string.Format("{0}.{1}", elementName, propertyName);
}

Thanks for helping me!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文