Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在xml文件中写入点符号?_C#_Xml_Xmlwriter_Writing - Fatal编程技术网

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

C# 如何在xml文件中写入点符号?,c#,xml,xmlwriter,writing,C#,Xml,Xmlwriter,Writing,我正在使用XmlWriter用C#编写一个XML文件。我可以使用writeStarteElement()方法编写元素,并且可以使用WriteAttributeString()方法向该元素添加属性。但如果我想用点符号添加属性,那么怎么做呢 <Element Attribute1="Value"> <Element.Attribute2> //How can i add attribute in this Notation.

我正在使用XmlWriter用C#编写一个XML文件。我可以使用
writeStarteElement()
方法编写元素,并且可以使用
WriteAttributeString()
方法向该元素添加属性。但如果我想用点符号添加属性,那么怎么做呢

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

谢谢你的帮助

还有什么比writeStarteElement(“Element.Attribute”)更干净的呢?它准确地描述了您正在做的事情——创建一个具有该名称的新元素

如果您确实想使用
XmlWriter
,我会坚持这种方法。然而,正如Henrik所说,LINQ到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的层次结构中吗?

有什么比
writeStarteElement(“Element.Attribute”)
更干净呢?它准确地描述了您正在做的事情——创建一个具有该名称的新元素

如果您确实想使用
XmlWriter
,我会坚持这种方法。然而,正如Henrik所说,LINQ到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的层次结构中吗?

没有“点表示法”这样的东西。您似乎指的是XAML。除了XAML,XML中没有“点表示法”这样的东西。这就是为什么你找不到任何对它的支持——它根本不存在。

没有“点符号”这样的东西。您似乎指的是XAML。除了XAML,XML中没有“点表示法”这样的东西。这就是为什么您找不到对它的任何支持-它不存在。

最后,我以这种方式使用这个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);
}

谢谢你帮助我

最后,我以这种方式使用这个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);
}

谢谢你帮助我

在将对象保存到Xml中时,我无法使用反射获取“Element.Attribute2”,或者每次都需要手动编写,或者需要执行一些奇怪的操作,如string.Format(“{0}.{1}”、elementName、attributeName),这一点都不好,为什么“一点都不好”?再说一次,它说的正是你的意思。我恐怕还不清楚你对这件事有什么不满。。。或者你为什么要首先使用这个符号。你在评论中提到了反思,但这是我们第一次听说。。。你想干什么?请给我们一个更大的图景。也许我没有更好地解释事情的方式,我应该,谢谢你的帮助。我再次更新了问题,试图解释要点!!对不起,我对你的格式还不是很清楚。。。我也不明白为什么您如此反对在需要时显式指定元素名称的两部分。对我来说,这似乎是一个简单的解决方案。虽然将对象保存到Xml中,但我无法使用反射获取“Element.Attribute2”,要么每次都需要手动编写,要么需要执行一些奇怪的操作,如string.Format(“{0}.{1}”,elementName,attributeName),这根本不好,为什么“一点都不好”?再说一次,它说的正是你的意思。我恐怕还不清楚你对这件事有什么不满。。。或者你为什么要首先使用这个符号。你在评论中提到了反思,但这是我们第一次听说。。。你想干什么?请给我们一个更大的图景。也许我没有更好地解释事情的方式,我应该,谢谢你的帮助。我再次更新了问题,试图解释要点!!对不起,我对你的格式还不是很清楚。。。我也不明白为什么您如此反对在需要时显式指定元素名称的两部分。对我来说,这似乎是一个简单的解决方案。它也在XMI版本1中使用。它是相同的“符号”吗?点是什么意思?它也在XMI版本1中使用。它是相同的“符号”吗?这些点是什么意思?