1

Sorry if the name of the thread is unclear. I had no idea how to form it properly. Anyways.

I have a class.

Class Overflow {
public string Name;
public decimal MoneySpent;
public string Code;
}

Then I have an XML that has value

<Data>
    <Name>John</LeidimoNr>
    <MoneySpent>78621.25</Code>
    <Code>XA-21456-sds</Code>
</Data>

So I need to put all of the XML values into the object Overflow.

What I'm doing is I'm iterating through Data

   foreach (XPathNavigator val in it)
   {
       var name = val.Name;
       var value = val.Value;
       var Type = Overflow.GetType();
       PropertyInfo info = Type.GetProperty(name);
       info.SetValue(Overflow, value, null);
   }

But the value is set to string and there's one value Decimal.

How do I make PropertyType same as in my class?

Wkjjjj
  • 45
  • 1
  • 8
  • Related Thread : http://stackoverflow.com/questions/10330155/c-sharp-xml-serialization-and-decimal-value –  Jun 29 '16 at 10:57

2 Answers2

2

Instead of re-inventing the wheel, why not use the built-in XmlSerializer to do this?

Add the relevant attributes to your class, and make your fields properties:

[XmlRoot("Data")]
public class Overflow
{
    [XmlElement]
    public string Name { get; set; }

    [XmlElement]
    public decimal MoneySpent { get; set; }

    [XmlElement]
    public string Code { get; set; }
}

Then create a serializer and pass it the XML source:

var serializer = new XmlSerializer(typeof(Overflow));
var obj = (Overflow) serializer.Deserialize(reader);

See this fiddle for a working demo.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • Well the problem with this solution was that I'm using DeserializeReply(message, object[] parameters) and it doesn't have the normal "XML" and I don't nkow how to turn that "message" that I'm given into reader. Only figured it out with var doc = new XPathDocument(message.GetReaderAtBodyContents()); – Wkjjjj Jun 29 '16 at 11:09
  • @Wkjjjj `GetReaderAtBodyContents` returns an `XmlReader`. You can pass that to `serializer.Deserialize`. – Charles Mager Jun 29 '16 at 11:17
  • "There is an error in XML document (1, 620)." {" was not expected."} Somehow it spits an error. – Wkjjjj Jun 29 '16 at 11:24
  • Then your message body and your class don't match - it evidently starts with `Response` rather than `Overflow`, so you'd need to have a class that matched the entirety of the response. I'm sure generally there must be built-in ways to solve this issue, but I'm not familiar enough with WCF to advise.. – Charles Mager Jun 29 '16 at 11:29
  • Well. The whole problem is that the people I'm dealing with can't fix their WSDL and I have to Serialize everything like I didin't have an WSDL. There's loads of different WebService calls and I have to catch them all. It's really a hassle to explain so much so I'll just suck it up and manually code the putting to objects. – Wkjjjj Jun 29 '16 at 11:33
1

That should do the trick

info.SetValue(Overflow, Convert.ChangeType(value, Type.PropertyType), null);

Convert.ChangeType documentation

Mantas Čekanauskas
  • 2,218
  • 6
  • 23
  • 43