简体   繁体   English

C#中XML元素的元素/属性注释

[英]XML Element's element/attribute annotation in C#

Is that possible to get an element's (child) element or an element's own attribute via xml annotation without phantom classes? 是否可以通过不带幻象类的xml注释获取元素的(子)元素或元素的自身属性?

example current code snipet: XML: 当前代码段示例:XML:

<root>
    ...
    <epno type="1">12</epno>
    ...
</root>

C# classes: C#类:

[XmlRoot("root")]
class Root {
    ...
    [XmlElement("epno")]
    public Epno epno;
    ...
}

class Epno { //"Phantom" class
    [XmlAttribute("type")]
    public int type;
    [XmlText]
    public int epno;
}

What I want is to remove that Epno class and move those props into the Root class... 我想要的是删除该Epno类并将那些道具移到Root类中...

[XmlRoot("root")]
class Root {
    ...
    [XmlElement("epno")]
    [XmlAttribute("type")] // I need a solution for this...
    public int type;
    [XmlElement("epno")]
    public int epno;
    ...
}

Also theres another place, where is a plus level, there I want to get an element's attribute, which is an another element... Than I want to get the element's element value. 还有另一个地方,在一个加号级别上,我想获取一个元素的属性,这是另一个元素...比我想要获得该元素的元素值。

For this an Xml example: 为此,一个Xml示例:

<root>
    <rates>
        <permanent votes="100">6.54</permanent>
        <temprate votes="100">6.54</temprate>
    </rates>
</root>

Here I want to put those values in to the root class, but in this case, there's need minimum 2 classes for parse it. 在这里,我想将这些值放入根类,但是在这种情况下,至少需要2个类才能对其进行解析。

So, there's exist a way to deserialize these classes via annotation without these phantom classes and without writing my own xml parser? 因此,有一种方法可以通过注释来反序列化这些类,而无需这些幻像类并且无需编写自己的xml解析器?

There is no way to get "one child element and its attributes" of a root class and transform these into elements of its root class during XML Serialization by using only XML Serialization Attributes. 无法获得根类的“一个子元素及其属性”,而仅通过 XML序列化属性将其转换为XML序列化过程中其根类的元素。

But, You can archieve desired result by: 但是,您可以通过以下方式归档所需的结果:

I think what you are looking for is XmlDocument. 我认为您正在寻找的是XmlDocument。

This does your second task: 这是您的第二项任务:

XmlDocument xDoc = new XmlDocument();

xDoc.LoadXml("<root><rates> <permanent votes = \"100\" > 6.54 </permanent>  <temprate votes = \"100\" > 6.54 </temprate></rates> </root> ");

// find the nodes we are interested in
XmlNode Rates = xDoc.SelectSingleNode("//rates");
XmlNode Root = xDoc.SelectSingleNode("root");

// We can't modify a collection live so create a temporary list
List<XmlNode> TempList = new List<XmlNode>();

foreach (XmlNode Node in Rates.ChildNodes)
{
    TempList.Add(Node);          
}

// remove the nodes and their container node
foreach (XmlNode Node in TempList)
{
    Node.ParentNode.RemoveChild(Node);
}
// Use this to remove the parent and children
// in one step
// Rates.ParentNode.RemoveChild(Rates); 

// insert in desired location
foreach (XmlNode Node in TempList)
{
    Root.AppendChild(Node);
}

// Hope this works!
xDoc.Save("C:\\test.xml");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM