1 // =================================
  2 // Copyright (c) 2024 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.Concepts;
  8 using System.Collections;
  9 using System.XPath;
 10 
 11 namespace System.Xml.Serialization
 12 {
 13     public concept XmlConstructible<T>{
 14     {
 15         where DefaultConstructible<T>;
 16         Result<bool> T.FromXml(System.Xml.Element* element);
 17         Result<bool> T.FromXml(System.Xml.Element* elementXmlSerializationContext& ctx);}
 18     }
 19 
 20     public concept XmlImportableScalarType<T>{
 21     {
 22         where
 23             T is bool or 
 24             T is sbyte or 
 25             T is byte or 
 26             T is short or 
 27             T is ushort or 
 28             T is int or 
 29             T is uint or 
 30             T is long or 
 31             T is ulong or 
 32             T is float or 
 33             T is double or 
 34             T is char or 
 35             T is wchar or 
 36             T is uchar or 
 37             T is string or 
 38             T is wstring or 
 39             T is ustring or 
 40             T is System.Uuid or 
 41             T is System.Date or 
 42             T is System.DateTime or 
 43             T is System.TimePoint or 
 44             T is System.Duration;}
 45     }
 46 
 47     public concept XmlImportableEnumeratedType<T>{
 48     {
 49         where System.Meta.IsEnumeratedType<T>();}
 50     }
 51 
 52     public Result<System.Xml.Element*> GetXmlFieldElement(const string& fieldNameSystem.Xml.Element* fromElement)
 53     {
 54         Result<UniquePtr<NodeSet>> result = EvaluateToNodeSet(fieldNamefromElement);
 55         if (result.Error()) return Result<System.Xml.Element*>(ErrorId(result.GetErrorId()));
 56         NodeSet* nodeSet = result.Value().Get();
 57         if (nodeSet->Count() == 1)
 58         {
 59             System.Xml.Node* node = nodeSet->GetNode(0);
 60             if (node->IsElementNode())
 61             {
 62                 System.Xml.Element* fieldElement = cast<System.Xml.Element*>(node);
 63                 return Result<System.Xml.Element*>(fieldElement);
 64             }
 65         }
 66         return Result<System.Xml.Element*>(cast<System.Xml.Element*>(null));
 67     }
 68 
 69     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxbool& value)
 70     {
 71         if (element != null)
 72         {
 73             auto result = ParseBool(element->GetAttribute("value"));
 74             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
 75             value = result.Value();
 76         }
 77         return Result<bool>(true);
 78     }
 79 
 80     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamebool& value)
 81     {
 82         auto result = GetXmlFieldElement(fieldNameparentElement);
 83         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
 84         System.Xml.Element* element = result.Value();
 85         return FromXml(elementctxvalue);
 86     }
 87 
 88     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxsbyte& value)
 89     {
 90         if (element != null)
 91         {
 92             auto result = ParseSByte(element->GetAttribute("value"));
 93             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
 94             value = result.Value();
 95         }
 96         return Result<bool>(true);
 97     }
 98 
 99     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamesbyte& value)
100     {
101         auto result = GetXmlFieldElement(fieldNameparentElement);
102         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
103         System.Xml.Element* element = result.Value();
104         return FromXml(elementctxvalue);
105     }
106 
107     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxbyte& value)
108     {
109         if (element != null)
110         {
111             auto result = ParseByte(element->GetAttribute("value"));
112             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
113             value = result.Value();
114         }
115         return Result<bool>(true);
116     }
117 
118     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamebyte& value)
119     {
120         auto result = GetXmlFieldElement(fieldNameparentElement);
121         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
122         System.Xml.Element* element = result.Value();
123         return FromXml(elementctxvalue);
124     }
125 
126     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxshort& value)
127     {
128         if (element != null)
129         {
130             auto result = ParseShort(element->GetAttribute("value"));
131             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
132             value = result.Value();
133         }
134         return Result<bool>(true);
135     }
136 
137     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameshort& value)
138     {
139         auto result = GetXmlFieldElement(fieldNameparentElement);
140         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
141         System.Xml.Element* element = result.Value();
142         return FromXml(elementctxvalue);
143     }
144 
145     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxushort& value)
146     {
147         if (element != null)
148         {
149             auto result = ParseUShort(element->GetAttribute("value"));
150             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
151             value = result.Value();
152         }
153         return Result<bool>(true);
154     }
155 
156     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameushort& value)
157     {
158         auto result = GetXmlFieldElement(fieldNameparentElement);
159         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
160         System.Xml.Element* element = result.Value();
161         return FromXml(elementctxvalue);
162     }
163 
164     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxint& value)
165     {
166         if (element != null)
167         {
168             auto result = ParseInt(element->GetAttribute("value"));
169             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
170             value = result.Value();
171         }
172         return Result<bool>(true);
173     }
174 
175     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameint& value)
176     {
177         auto result = GetXmlFieldElement(fieldNameparentElement);
178         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
179         System.Xml.Element* element = result.Value();
180         return FromXml(elementctxvalue);
181     }
182 
183     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxuint& value)
184     {
185         if (element != null)
186         {
187             auto result = ParseUInt(element->GetAttribute("value"));
188             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
189             value = result.Value();
190         }
191         return Result<bool>(true);
192     }
193 
194     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameuint& value)
195     {
196         auto result = GetXmlFieldElement(fieldNameparentElement);
197         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
198         System.Xml.Element* element = result.Value();
199         return FromXml(elementctxvalue);
200     }
201 
202     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxlong& value)
203     {
204         if (element != null)
205         {
206             auto result = ParseLong(element->GetAttribute("value"));
207             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
208             value = result.Value();
209         }
210         return Result<bool>(true);
211     }
212 
213     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamelong& value)
214     {
215         auto result = GetXmlFieldElement(fieldNameparentElement);
216         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
217         System.Xml.Element* element = result.Value();
218         return FromXml(elementctxvalue);
219     }
220 
221     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxulong& value)
222     {
223         if (element != null)
224         {
225             auto result = ParseULong(element->GetAttribute("value"));
226             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
227             value = result.Value();
228         }
229         return Result<bool>(true);
230     }
231 
232     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameulong& value)
233     {
234         auto result = GetXmlFieldElement(fieldNameparentElement);
235         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
236         System.Xml.Element* element = result.Value();
237         return FromXml(elementctxvalue);
238     }
239 
240     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxfloat& value)
241     {
242         if (element != null)
243         {
244             auto result = ParseFloat(element->GetAttribute("value"));
245             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
246             value = result.Value();
247         }
248         return Result<bool>(true);
249     }
250 
251     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamefloat& value)
252     {
253         auto result = GetXmlFieldElement(fieldNameparentElement);
254         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
255         System.Xml.Element* element = result.Value();
256         return FromXml(elementctxvalue);
257     }
258 
259     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxdouble& value)
260     {
261         if (element != null)
262         {
263             auto result = ParseDouble(element->GetAttribute("value"));
264             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
265             value = result.Value();
266         }
267         return Result<bool>(true);
268     }
269 
270     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamedouble& value)
271     {
272         auto result = GetXmlFieldElement(fieldNameparentElement);
273         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
274         System.Xml.Element* element = result.Value();
275         return FromXml(elementctxvalue);
276     }
277 
278     public Result<bool> FromXml(Element* elementXmlSerializationContext& ctxchar& value)
279     {
280         if (element != null)
281         {
282             string result = element->GetAttribute("value");
283             if (result.IsEmpty())
284             {
285                 int errorId = AllocateError("result is empty");
286                 return Result<bool>(ErrorId(errorId));
287             }
288             value = result[0];
289         }
290         return Result<bool>(true);
291     }
292 
293     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamechar& value)
294     {
295         auto result = GetXmlFieldElement(fieldNameparentElement);
296         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
297         System.Xml.Element* element = result.Value();
298         return FromXml(elementctxvalue);
299     }
300 
301     public Result<bool> FromXml(Element* elementXmlSerializationContext& ctxwchar& value)
302     {
303         if (element != null)
304         {
305             string valueAttr = element->GetAttribute("value");
306             if (valueAttr.IsEmpty())
307             {
308                 int errorId = AllocateError("valueAttr is empty");
309                 return Result<bool>(ErrorId(errorId));
310             }
311             auto utf16Result = ToUtf16(valueAttr);
312             if (utf16Result.Error())
313             {
314                 return Result<bool>(ErrorId(utf16Result.GetErrorId()));
315             }
316             value = utf16Result.Value()[0];
317         }
318         return Result<bool>(true);
319     }
320 
321     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamewchar& value)
322     {
323         auto result = GetXmlFieldElement(fieldNameparentElement);
324         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
325         System.Xml.Element* element = result.Value();
326         return FromXml(elementctxvalue);
327     }
328 
329     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxuchar& value)
330     {
331         if (element != null)
332         {
333             string valueAttr = element->GetAttribute("value");
334             if (valueAttr.IsEmpty())
335             {
336                 int errorId = AllocateError("valueAttr is empty");
337                 return Result<bool>(ErrorId(errorId));
338             }
339             auto utf32Result = ToUtf32(valueAttr);
340             if (utf32Result.Error())
341             {
342                 return Result<bool>(ErrorId(utf32Result.GetErrorId()));
343             }
344             value = utf32Result.Value()[0];
345         }
346         return Result<bool>(true);
347     }
348 
349     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameuchar& value)
350     {
351         auto result = GetXmlFieldElement(fieldNameparentElement);
352         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
353         System.Xml.Element* element = result.Value();
354         return FromXml(elementctxvalue);
355     }
356 
357     public Result<bool> FromXml<T>(System.Xml.Element* elementXmlSerializationContext& ctxT& value)
358         where T is XmlImportableEnumeratedType
359     {
360         if (element != null)
361         {
362             auto result = ParseLong(element->GetAttribute("value"));
363             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
364             long longValue = result.Value();
365             value = cast<T>(cast<T.UnderlyingType>(longValue));
366         }
367         return Result<bool>(true);
368     }
369 
370     public Result<bool> FromXml<T>(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameT& value)
371         where T is XmlImportableEnumeratedType
372     {
373         auto result = GetXmlFieldElement(fieldNameparentElement);
374         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
375         System.Xml.Element* element = result.Value();
376         return FromXml(elementctxvalue);
377     }
378 
379     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxSystem.Date& value)
380     {
381         if (element != null)
382         {
383             auto result = ParseDate(element->GetAttribute("value"));
384             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
385             value = result.Value();
386         }
387         return Result<bool>(true);
388     }
389 
390     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameSystem.Date& value)
391     {
392         auto result = GetXmlFieldElement(fieldNameparentElement);
393         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
394         System.Xml.Element* element = result.Value();
395         return FromXml(elementctxvalue);
396     }
397 
398     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxSystem.DateTime& value)
399     {
400         if (element != null)
401         {
402             auto result = ParseDateTime(element->GetAttribute("value"));
403             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
404             value = result.Value();
405         }
406         return Result<bool>(true);
407     }
408 
409     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameSystem.DateTime& value)
410     {
411         auto result = GetXmlFieldElement(fieldNameparentElement);
412         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
413         System.Xml.Element* element = result.Value();
414         return FromXml(elementctxvalue);
415     }
416 
417     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxstring& value)
418     {
419         if (element != null)
420         {
421             value = element->GetAttribute("value");
422         }
423         return Result<bool>(true);
424     }
425 
426     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamestring& value)
427     {
428         auto result = GetXmlFieldElement(fieldNameparentElement);
429         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
430         System.Xml.Element* element = result.Value();
431         return FromXml(elementctxvalue);
432     }
433 
434     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxwstring& value)
435     {
436         if (element != null)
437         {
438             string attributeValue = element->GetAttribute("value");
439             auto result = ToUtf16(attributeValue);
440             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
441             value = result.Value();
442         }
443         return Result<bool>(true);
444     }
445 
446     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNamewstring& value)
447     {
448         auto result = GetXmlFieldElement(fieldNameparentElement);
449         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
450         System.Xml.Element* element = result.Value();
451         return FromXml(elementctxvalue);
452     }
453 
454     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxustring& value)
455     {
456         if (element != null)
457         {
458             string attributeValue = element->GetAttribute("value");
459             auto result = ToUtf32(attributeValue);
460             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
461             value = result.Value();
462         }
463         return Result<bool>(true);
464     }
465 
466     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameustring& value)
467     {
468         auto result = GetXmlFieldElement(fieldNameparentElement);
469         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
470         System.Xml.Element* element = result.Value();
471         return FromXml(elementctxvalue);
472     }
473 
474     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxSystem.TimePoint& value)
475     {
476         if (element != null)
477         {
478             auto result = ParseLong(element->GetAttribute("value"));
479             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
480             long longValue = result.Value();
481             value = TimePoint(longValue);
482         }
483         return Result<bool>(true);
484     }
485 
486     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameSystem.TimePoint& value)
487     {
488         auto result = GetXmlFieldElement(fieldNameparentElement);
489         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
490         System.Xml.Element* element = result.Value();
491         return FromXml(elementctxvalue);
492     }
493 
494     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxSystem.Duration& value)
495     {
496         if (element != null)
497         {
498             auto result = ParseLong(element->GetAttribute("value"));
499             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
500             long longValue = result.Value();
501             value = Duration(longValue);
502         }
503         return Result<bool>(true);
504     }
505 
506     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameSystem.Duration& value)
507     {
508         auto result = GetXmlFieldElement(fieldNameparentElement);
509         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
510         System.Xml.Element* element = result.Value();
511         return FromXml(elementctxvalue);
512     }
513 
514     public Result<bool> FromXml(System.Xml.Element* elementXmlSerializationContext& ctxSystem.Uuid& value)
515     {
516         if (element != null)
517         {
518             auto result = ParseUuid(element->GetAttribute("value"));
519             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
520             value = result.Value();
521         }
522         return Result<bool>(true);
523     }
524 
525     public Result<bool> FromXml(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameSystem.Uuid& value)
526     {
527         auto result = GetXmlFieldElement(fieldNameparentElement);
528         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
529         System.Xml.Element* element = result.Value();
530         return FromXml(elementctxvalue);
531     }
532 
533     public Result<bool> FromXml<T>(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameT& object)
534         where T is XmlConstructible
535     {
536         auto result = GetXmlFieldElement(fieldNameparentElement);
537         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
538         System.Xml.Element* element = result.Value();
539         if (element != null)
540         {
541             return object.FromXml(elementctx);
542         }
543         return Result<bool>(true);
544     }
545 
546     public Result<bool> FromXml<T>(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameUniquePtr<T>& object)
547         where T is XmlConstructible
548     {
549         object.Reset();
550         auto result = GetXmlFieldElement(fieldNameparentElement);
551         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
552         System.Xml.Element* element = result.Value();
553         if (element != null)
554         {
555             string value = element->GetAttribute("value");
556             if (value != "null")
557             {
558                 string classIdAttr = element->GetAttribute("classId");
559                 if (!classIdAttr.IsEmpty())
560                 {
561                     auto result = ParseInt(classIdAttr);
562                     if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
563                     int classId = result.Value();
564                     Result<XmlSerializable*> objectResult = XmlClassRegistry.Instance().Create(classId);
565                     if (objectResult.Error()) return Result<bool>(ErrorId(objectResult.GetErrorId()));
566                     object.Reset(cast<T*>(objectResult.Value()));
567                     return object->FromXml(elementctx);
568                 }
569             }
570         }
571         return Result<bool>(true);
572     }
573 
574     public Result<bool> FromXml<T>(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameSharedPtr<T>& object)
575         where T is XmlConstructible
576     {
577         object.Reset();
578         auto result = GetXmlFieldElement(fieldNameparentElement);
579         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
580         System.Xml.Element* element = result.Value();
581         if (element != null)
582         {
583             string value = element->GetAttribute("value");
584             if (value != "null")
585             {
586                 string classIdAttr = element->GetAttribute("classId");
587                 if (!classIdAttr.IsEmpty())
588                 {
589                     auto result = ParseInt(classIdAttr);
590                     if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
591                     int classId = result.Value();
592                     Result<XmlSerializable*> objectResult = XmlClassRegistry.Instance().Create(classId);
593                     if (objectResult.Error()) return Result<bool>(ErrorId(objectResult.GetErrorId()));
594                     object.Reset(cast<T*>(objectResult.Value()));
595                     return object->FromXml(elementctx);
596                 }
597             }
598         }
599         return Result<bool>(true);
600     }
601 
602     public Result<bool> FromXml<T>(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameXmlPtr<T>& ptr)
603         where T is XmlConstructible
604     {
605         ptr.Reset();
606         auto result = GetXmlFieldElement(fieldNameparentElement);
607         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
608         System.Xml.Element* element = result.Value();
609         if (element != null)
610         {
611             string value = element->GetAttribute("value");
612             if (value != "null")
613             {
614                 string objectIdAttr = element->GetAttribute("objectId");
615                 if (!objectIdAttr.IsEmpty())
616                 {
617                     auto result = ParseUuid(objectIdAttr);
618                     if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
619                     Uuid objectId = result.Value();
620                     ptr.SetTargetObjectId(objectId);
621                     XmlBundle* xmlBundle = ctx.GetXmlBundle();
622                     if (xmlBundle != null)
623                     {
624                         xmlBundle->AddPtr(&ptr);
625                     }
626                 }
627             }
628         }
629         return Result<bool>(true);
630     }
631 
632     public Result<bool> FromXml<T>(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameList<T>& list)
633         where T is XmlImportableScalarType
634     {
635         list.Clear();
636         auto result = GetXmlFieldElement(fieldNameparentElement);
637         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
638         System.Xml.Element* element = result.Value();
639         if (element != null)
640         {
641             Result<UniquePtr<NodeSet>> result = EvaluateToNodeSet("item"element);
642             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
643             NodeSet* nodeSet = result.Value().Get();
644             int n = nodeSet->Count();
645             for (int i = 0; i < n; ++i;)
646             {
647                 System.Xml.Node* node = nodeSet->GetNode(i);
648                 if (node->IsElementNode())
649                 {
650                     System.Xml.Element* element = cast<System.Xml.Element*>(node);
651                     T value;
652                     auto result = FromXml(elementctxvalue);
653                     if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
654                     list.Add(Rvalue(value));
655                 }
656             }
657         }
658         return Result<bool>(true);
659     }
660 
661     public Result<bool> FromXml<T>(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameList<T>& list)
662         where T is XmlConstructible
663     {
664         list.Clear();
665         auto result = GetXmlFieldElement(fieldNameparentElement);
666         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
667         System.Xml.Element* element = result.Value();
668         if (element != null)
669         {
670             Result<UniquePtr<NodeSet>> result = EvaluateToNodeSet("item"element);
671             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
672             NodeSet* nodeSet = result.Value().Get();
673             int n = nodeSet->Count();
674             for (int i = 0; i < n; ++i;)
675             {
676                 System.Xml.Node* node = nodeSet->GetNode(i);
677                 if (node->IsElementNode())
678                 {
679                     System.Xml.Element* element = cast<System.Xml.Element*>(node);
680                     T object;
681                     auto result = object.FromXml(elementctx);
682                     if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
683                     list.Add(Rvalue(object));
684                 }
685             }
686         }
687         return Result<bool>(true);
688     }
689 
690     public Result<bool> FromXml<T>(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameList<UniquePtr<T>>& list)
691         where T is XmlConstructible
692     {
693         list.Clear();
694         auto result = GetXmlFieldElement(fieldNameparentElement);
695         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
696         System.Xml.Element* element = result.Value();
697         if (element != null)
698         {
699             Result<UniquePtr<NodeSet>> result = EvaluateToNodeSet("item"element);
700             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
701             NodeSet* nodeSet = result.Value().Get();
702             int n = nodeSet->Count();
703             for (int i = 0; i < n; ++i;)
704             {
705                 System.Xml.Node* node = nodeSet->GetNode(i);
706                 if (node->IsElementNode())
707                 {
708                     System.Xml.Element* element = cast<System.Xml.Element*>(node);
709                     UniquePtr<T> object;
710                     string value = element->GetAttribute("value");
711                     if (value != "null")
712                     {
713                         string classIdAttr = element->GetAttribute("classId");
714                         if (!classIdAttr.IsEmpty())
715                         {
716                             auto result = ParseInt(classIdAttr);
717                             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
718                             int classId = result.Value();
719                             Result<XmlSerializable*> objectResult = XmlClassRegistry.Instance().Create(classId);
720                             if (objectResult.Error()) return Result<bool>(ErrorId(objectResult.GetErrorId()));
721                             object.Reset(cast<T*>(objectResult.Value()));
722                             auto xmlResult = object->FromXml(elementctx);
723                             if (xmlResult.Error()) return Result<bool>(ErrorId(xmlResult.GetErrorId()));
724                         }
725                     }
726                     list.Add(Rvalue(object));
727                 }
728             }
729         }
730         return Result<bool>(true);
731     }
732 
733     public Result<bool> FromXml<T>(System.Xml.Element* parentElementXmlSerializationContext& ctxconst string& fieldNameList<SharedPtr<T>>& list)
734         where T is XmlConstructible
735     {
736         list.Clear();
737         auto result = GetXmlFieldElement(fieldNameparentElement);
738         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
739         System.Xml.Element* element = result.Value();
740         if (element != null)
741         {
742             Result<UniquePtr<NodeSet>> result = EvaluateToNodeSet("item"element);
743             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
744             NodeSet* nodeSet = result.Value().Get();
745             int n = nodeSet->Count();
746             for (int i = 0; i < n; ++i;)
747             {
748                 System.Xml.Node* node = nodeSet->GetNode(i);
749                 if (node->IsElementNode())
750                 {
751                     System.Xml.Element* element = cast<System.Xml.Element*>(node);
752                     SharedPtr<T> object;
753                     string value = element->GetAttribute("value");
754                     if (value != "null")
755                     {
756                         string classIdAttr = element->GetAttribute("classId");
757                         if (!classIdAttr.IsEmpty())
758                         {
759                             auto result = ParseInt(classIdAttr);
760                             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
761                             int classId = result.Value();
762                             Result<XmlSerializable*> objectResult = XmlClassRegistry.Instance().Create(classId);
763                             if (objectResult.Error()) return Result<bool>(ErrorId(objectResult.GetErrorId()));
764                             object.Reset(cast<T*>(objectResult.Value()));
765                             auto xmlResult = object->FromXml(elementctx);
766                             if (xmlResult.Error()) return Result<bool>(ErrorId(xmlResult.GetErrorId()));
767                         }
768                     }
769                     list.Add(Rvalue(object));
770                 }
771             }
772         }
773         return Result<bool>(true);
774     }
775 
776     public Result<bool> FromXml<T>(Element* parentElementXmlSerializationContext& ctxconst string& fieldNameList<XmlPtr<T>>& list)
777         where T is XmlConstructible
778     {
779         list.Clear();
780         auto result = GetXmlFieldElement(fieldNameparentElement);
781         if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
782         System.Xml.Element* element = result.Value();
783         if (element != null)
784         {
785             Result<UniquePtr<NodeSet>> result = EvaluateToNodeSet("item"element);
786             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
787             NodeSet* nodeSet = result.Value().Get();
788             int n = nodeSet->Count();
789             list.Reserve(n);
790             for (int i = 0; i < n; ++i;)
791             {
792                 System.Xml.Node* node = nodeSet->GetNode(i);
793                 if (node->IsElementNode())
794                 {
795                     System.Xml.Element* element = cast<System.Xml.Element*>(node);
796                     list.Add(XmlPtr<T>());
797                     XmlPtr<T>& ptr = list.Back();
798                     string value = element->GetAttribute("value");
799                     if (value != "null")
800                     {
801                         string objectIdAttr = element->GetAttribute("objectId");
802                         if (!objectIdAttr.IsEmpty())
803                         {
804                             auto result = ParseUuid(objectIdAttr);
805                             if (result.Error()) return Result<bool>(ErrorId(result.GetErrorId()));
806                             Uuid objectId = result.Value();
807                             ptr.SetTargetObjectId(objectId);
808                             XmlBundle* xmlBundle = ctx.GetXmlBundle();
809                             if (xmlBundle != null)
810                             {
811                                 xmlBundle->AddPtr(&ptr);
812                             }
813                         }
814                     }
815                 }
816             }
817         }
818         return Result<bool>(true);
819     }
820 }