Deserializacja XML -> Obiekt

Deserializacja XML -> Obiekt
BC
  • Rejestracja:około 5 lat
  • Ostatnio:3 dni
  • Postów:11
0

Mam xml który może mieć różne i niewiadome root'y, namespace'y (RandomRoot10 xmlns="www.random2077.xsd"), czy nazwy pól (RandomElement101)

Kopiuj
<RandomRoot10 xmlns="www.random2077.xsd" time="2021-02-01T20:06:44.411+01:00" randomBool="1" code="10" name="Random Category">
	<RandomElement101 id="1" Value="00" text="random text 1" />
	<RandomElement101 id="2" Value="01" text="random text 2" />
	<RandomElement101 id="3" Value="02" text="random text 3"/>
	<RandomElement101 id="4" Value="03" text="random text 4" />
	<RandomElement101 id="5" Value="04" text="random text 5"/>
</RandomRoot10>

I nie wiem jak klasę uniwersalną zrobić (a raczej deserializacje dla takiego xml), jedyne co wiem to że ID to int, Value to string itp.

Próbowałem tak (dla pojedynczego RandomElementu jak na razie) ale dalej krzyczy przy tym xmlns :

Kopiuj

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(text); // xml w stringu here

                var tags = doc.GetElementsByTagName("RandomElement101"); // ??

                var st = outer.Item(0).OuterXml;

                using (TextReader textReader = new StringReader(st))
                {
                    using (XmlTextReader reader = new XmlTextReader(textReader))
                    {
                        //reader.Namespaces = false;

                        XmlSerializer serializer = new XmlSerializer(typeof(RandomClass));
                        var obj = (RandomClass)serializer.Deserialize(reader); // Exception
                    }
                }

Exception przy Deserialize :

There is an error in XML document (1, 2), bo dalej xmlns widzi nawet dla pojedynczego wiersza :

<randomelement101 id="1" text="random text 1" value="00" />

Umie ktoś lepiej w te xmle ?

edytowany 1x, ostatnio: BoomC
WeiXiao
  • Rejestracja:około 9 lat
  • Ostatnio:około 7 godzin
  • Postów:5146
0

XML2Class online wyrzucił takie coś

Kopiuj
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
	[XmlRoot(ElementName="RandomElement101", Namespace="www.random2077.xsd")]
	public class RandomElement101 
	{
		[XmlAttribute(AttributeName="id")]
		public string Id { get; set; }
		
		[XmlAttribute(AttributeName="Value")]
		public string Value { get; set; }
		
		[XmlAttribute(AttributeName="text")]
		public string Text { get; set; }
	}

	[XmlRoot(ElementName="RandomRoot10", Namespace="www.random2077.xsd")]
	public class RandomRoot10
	{
		[XmlElement(ElementName="RandomElement101", Namespace="www.random2077.xsd")]
		public List<RandomElement101> RandomElement101 { get; set; }
		
		[XmlAttribute(AttributeName="xmlns")]
		public string Xmlns { get; set; }
		
		[XmlAttribute(AttributeName="time")]
		public string Time { get; set; }
		
		[XmlAttribute(AttributeName="randomBool")]
		public string RandomBool { get; set; }
		
		[XmlAttribute(AttributeName="code")]
		public string Code { get; set; }
		
		[XmlAttribute(AttributeName="name")]
		public string Name { get; set; }
	}

}

a jak nie, to jeszcze inny:

Kopiuj
[XmlRoot(ElementName="RandomElement101")]
public class RandomElement101 { 

	[XmlAttribute(AttributeName="id")] 
	public int Id { get; set; } 

	[XmlAttribute(AttributeName="Value")] 
	public int Value { get; set; } 

	[XmlAttribute(AttributeName="text")] 
	public string Text { get; set; } 
}

[XmlRoot(ElementName="RandomRoot10")]
public class RandomRoot10 { 

	[XmlElement(ElementName="RandomElement101")] 
	public List<RandomElement101> RandomElement101 { get; set; } 

	[XmlAttribute(AttributeName="xmlns")] 
	public string Xmlns { get; set; } 

	[XmlAttribute(AttributeName="time")] 
	public DateTime Time { get; set; } 

	[XmlAttribute(AttributeName="randomBool")] 
	public int RandomBool { get; set; } 

	[XmlAttribute(AttributeName="code")] 
	public int Code { get; set; } 

	[XmlAttribute(AttributeName="name")] 
	public string Name { get; set; } 
}
Kopiuj
using System.Xml.Serialization;
XmlSerializer serializer = new XmlSerializer(typeof(RandomRoot10));
using (StringReader reader = new StringReader(xml))
{
   var test = (RandomRoot10)serializer.Deserialize(reader);
}
edytowany 1x, ostatnio: WeiXiao
BC
  • Rejestracja:około 5 lat
  • Ostatnio:3 dni
  • Postów:11
0

Dużo to nie da niestety, bo problem w tym że następny xml będzie inny i nie mogę klasy nazwać RandomRoot10 bo zaraz będzie mogła mieć nazwę RandomRoot55 i tak samo Namespace="www.random2077.xsd" może być całkiem inny.

Jutro jeszcze spróbuję sobie element po elemencie i na sztywno przepisać schodząc niżej do XmlNode i niżej aż do konkretnych value.
Chyba że ktoś ma inny pomysł.

edytowany 1x, ostatnio: BoomC
Ales
  • Rejestracja:około 6 lat
  • Ostatnio:około 10 godzin
  • Postów:121
1

Spróbuj przekonwertować xml do dynamic i potem pętla foreach na właściwościach lub dictionary

BC
  • Rejestracja:około 5 lat
  • Ostatnio:3 dni
  • Postów:11
0

@Ales: Chyba to będzie jedyne rozwiązanie, próbowałem usuwać Namespace i to niby działa, ale przy Deserializacji wymagana jest poprawna nazwa klasy równa tej z XmlRoot/XmlElement. Inaczej wali błędem.
Trzeba ręcznie wyciągać wartości i tworzyć obiekt. Ewentualnie XElementy->XAttribute->Name + refleksja też myślę że by zadziałało.

Ales
  • Rejestracja:około 6 lat
  • Ostatnio:około 10 godzin
  • Postów:121
0

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.