1

Is that possible to get an element's (child) element or an element's own attribute via xml annotation without phantom classes?

example current code snipet: XML:

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

C# classes:

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

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

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

So, there's exist a way to deserialize these classes via annotation without these phantom classes and without writing my own xml parser?

golddragon007
  • 1,247
  • 14
  • 27
  • I'm sorry, but what does you mean by "without phantom classes"? – Cleiton Dec 30 '15 at 22:32
  • see the second code segment, I mean under phantom classes which are basically unnecessary to create because it can contains the same information the root/parent class too and if you save to DB your data, you won't create for that data a new table, only simply join it to your "original" data. In the second code fragment the Epno class is a phantom because it's useless to put into a new class that two data... – golddragon007 Dec 30 '15 at 22:40
  • Or, I can say this too: I want to create the code fragment 3 from the fragment 2, the original code which I need to deserialize is in the fragment 1. Fragment 4 is another data which I need to deserialize, but that's level 2... – golddragon007 Dec 30 '15 at 22:45

2 Answers2

0

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.

But, You can archieve desired result by:

Community
  • 1
  • 1
Cleiton
  • 17,663
  • 13
  • 46
  • 59
  • I'm afraid from this.... Basically I need this because I want to put it into a DB these data, but a bit more normal format. So what will happen, If I create a new (or more) property with XMLIgnore annotation and I linking to these "phantom" props together with the new props? (at get I need to read if exist and at set I need to create a class if not exist) And in this case I put SQLIgnore to the phantom class... is this a suitable solution? What you think? – golddragon007 Dec 31 '15 at 00:54
0

I think what you are looking for is 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");
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69