Entwickler-Ecke

IO, XML und Registry - Klassenstruktur in XML speichern und abändern


katruk - Sa 04.10.08 14:06
Titel: Klassenstruktur in XML speichern und abändern
Hallo!

Habe folgndes Problem:

mit dem XML-Serialize kriege ich aus meiner Klassenstruktur diese Datei:

XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
  <?xml version="1.0" ?> 
- <token xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="s4" Startswith="1" Endswith="2" Exist="3" Minoccures="4" Maxoccures="5">
- <Properties>
  <Property Name="p1" Startswith="11" Exist="1" Minoccures="1" Maxoccures="1" Type="11" /> 
  </Properties>
- <Group>
- <GroupProperty Name="g1" Exist="11" Minoccures="1" Maxoccures="1">
- <Properties>
  <Property Name="pg1" Startswith="1" Exist="1" Minoccures="1" Maxoccures="1" Type="1" /> 
  </Properties>
  </GroupProperty>
  </Group>
  </token>


Will aber die Daten in dieser Form kriegen und meine Klassenstruktur behalten:

XML-Daten
1:
2:
3:
4:
5:
6:
- <token Name="s4" Startswith="1" Endswith="2" Exist="3" Minoccures="4" Maxoccures="5">
  <Property Name="p1" Startswith="11" Exist="1" Minoccures="1" Maxoccures="1" Type="11" /> 
- <Group Name="g1" Exist="11" Minoccures="1" Maxoccures="1">
  <Property Name="pg1" Startswith="1" Exist="1" Minoccures="1" Maxoccures="1" Type="1" /> 
  </Group>
  </token>


Klassenstruktur:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
 [XmlRoot("token")]
   public class Segment : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

      
        #region Variables
        private string _name = string.Empty;
        private string _startswith = string.Empty;
        private string _endswith = string.Empty;
        private string _exist = string.Empty;
        private string _minoccures = string.Empty;
        private string _maxoccures = string.Empty;


        private List<Property> _properties = null;
        private List<GroupProperty> _groupproperties = null;
      ...
    }
public class Property
    {

        #region Variables
        private string _name = string.Empty;
        private string _startswith = string.Empty;
        private string _exist = string.Empty;
        private string _minoccures = string.Empty;
        private string _maxoccures = string.Empty;
        private string _type = string.Empty;
...
}
public class GroupProperty 
    {

        #region Variables
        private string _name = string.Empty;
        private string _exist = string.Empty; 
        private string _minoccures = string.Empty;
        private string _maxoccures = string.Empty;

        private List<Property> _properties = null;
...
}


Wie kann man die Klassennamen und xml:ns Infos ausblenden?


Kha - Sa 04.10.08 15:57

Du hast in jeder Klasse die gleichen Felder deklariert, das ist doch ein gutes Zeichen dafür, dass in deiner Klassenhierarchie etwas nicht stimmt. Du solltest alle drei Klassen (oder zumindest Property und PropertyGroup) von einer Basisklasse ableiten, ich nenne sie mal PropertyBase. Segment sieht dann etwas so aus:

C#-Quelltext
1:
2:
3:
4:
5:
public class Segment : PropertyBase {
    [XmlElement(DataType = typeof(Property)),
    XmlElement(DataType = typeof(PropertyGroup))]
    public List<PropertyBase> Childs { get = ... };
 }
Die Struktur sollte dann passen. Ich glaube aber nicht, dass du das "xmlns" unterdrücken kannst, wahrscheinlich musst du das einfach danach von Hand entfernen.


katruk - So 05.10.08 18:44

Hallo Kha!

Danke für deine Antwort. Die Klassenstruktur, die Du vorgeschlagen hast, löst aber das Problem mit den <Properties></Properties> innerhalb der Gruppe nicht, oder? Da die Properties in der Klasse GroupProperty auch als List<Property> deklariert werden sollen, wird die XML-datei dann so aussehen:

XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
 
 <?xml version="1.0" ?> 
- <token xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="s4" Startswith="1" Endswith="2" Exist="3" Minoccures="4" Maxoccures="5">
  <Property Name="p1" Startswith="11" Exist="1" Minoccures="1" Maxoccures="1" Type="11" /> 
- <Group Name="g1" Exist="11" Minoccures="1" Maxoccures="1">
- <span style="color: red"><Properties></span>
  <Property Name="pg1" Startswith="1" Exist="1" Minoccures="1" Maxoccures="1" Type="1" /> 
  <span style="color: red"></Properties></span>
  </Group>
  </token>


oder?
Gibts irgendeine Direktive an XML, die die Klassennamen ausblenden bw. umbennen lässt?

Danke!


katruk - So 05.10.08 19:06

Hallo!

Habe jetzt die Klassenstruktur geändert und kriege diese Datei:


XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
  <?xml version="1.0" ?> 
- <token xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="s6" Startswith="1" Endswith="1" Exist="1" Minoccures="1" Maxoccures="1">
- <_childs>
  <BasisProperty xsi:type="Property" Name="2" Startswith="2" Endswith="" Exist="2" Minoccures="2" Maxoccures="2" Type="2" /> 
- <BasisProperty xsi:type="GroupProperty" Name="3" Startswith="" Endswith="" Exist="3" Minoccures="3" Maxoccures="3">
- <Properties>
  <Property Name="4" Startswith="4" Endswith="" Exist="4" Minoccures="4" Maxoccures="4" Type="4" /> 
  <Property Name="5" Startswith="5" Endswith="" Exist="5" Minoccures="5" Maxoccures="5" Type="5" /> 
  </Properties>
  </BasisProperty>
  </_childs>
  <Property Name="2" Startswith="2" Endswith="" Exist="2" Minoccures="2" Maxoccures="2" Type="2" /> 
- <GroupProperty Name="3" Startswith="" Endswith="" Exist="3" Minoccures="3" Maxoccures="3">
- <Properties>
  <Property Name="4" Startswith="4" Endswith="" Exist="4" Minoccures="4" Maxoccures="4" Type="4" /> 
  <Property Name="5" Startswith="5" Endswith="" Exist="5" Minoccures="5" Maxoccures="5" Type="5" /> 
  </Properties>
  </GroupProperty>
  </token>

Teil unte sieht schon besser aus bis auf <Properties>
</Properties> Teil in der Group. Wie kann ich das Teil mit <_childs> </_childs> unterdrücken?