0

I have this xml

<Report Name="Report">
  <Input>
    <Content>
      <XmlInput>Any xml inside the input tag should be deserialised as string</XmlInput>
      <XmlInput>Any xml inside the input tag should be deserialised as string</XmlInput>
    </Content>
    <!--<XmlInput>Any xml inside the input tag should be deserialised as string</XmlInput>-->
  </Input>
</Report>

and this class

[XmlRoot("Report")]
public class  Report
{
    [XmlAttribute]
    public string Name { get; set; }

    public Input Input { get; set; }
}

public class Input
{
    [XmlElement]
    public string Content { get; set; }
}

I am using the following code to deserialize the xml

        string path = @"C:\temp\myxml.xml";
        var xmlSerializer = new XmlSerializer(typeof(Report));
        using (var reader = new StreamReader(path))
        {
            var report = (Report)xmlSerializer.Deserialize(reader);
        }

The problem here is, I want the xml content inside the content element to be deserialized as a string. Is this possible?

 <Content>
  <XmlInput>Any xml inside the input tag should be deserialised as string</XmlInput>
  <XmlInput>Any xml inside the input tag should be deserialised as string</XmlInput>
</Content>
leppie
  • 115,091
  • 17
  • 196
  • 297
user1131926
  • 1,311
  • 2
  • 18
  • 32
  • So you want the two values in the two `XmlInput` elements concatenated and put in the `Input.Content` string? Or do you want the string to be the inner xml of the `Content` element? Either way you'll probably have to do custom serialization and deserialization. – juharr Mar 10 '15 at 12:13
  • Do you mean that you want to extract `Any xml inside the input tag should be deserialised as stringAny xml inside the input tag should be deserialised as string` as the result in your example? – Esoteric Screen Name Mar 10 '15 at 12:13
  • @EsotericScreenName yes. so the string will contain the xml – user1131926 Mar 10 '15 at 12:14
  • You could deserialize each `XmlInput` like normal and add code to the `Input` class that would recreate the inner xml instead. – juharr Mar 10 '15 at 12:19
  • @juharr, actually this is just an example. the content element can basically include any xml – user1131926 Mar 10 '15 at 12:24

1 Answers1

0

Doubt there's a way with deserialization... With Linq to XML it would look like this:

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load("XMLFile1.xml");

        IEnumerable<XElement> reportElements = doc.Descendants("Report");

        IEnumerable<Report> reports = reportElements
            .Select(e => new Report 
            { 
                Name = e.Attribute("Name").Value,
                Input = new Input
                {
                    Content = e.Element("Input").Element("Content").ToString()
                }
            });
    }
}

Edit

If you want to strip the content tag too:

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load("XMLFile1.xml");

        IEnumerable<XElement> reportElements = doc.Descendants("Report");

        IEnumerable<Report> reports = reportElements
            .Select(e => new Report
            {
                Name = e.Attribute("Name").Value,
                Input = new Input
                {
                    Content = string.Join("\n", e.Element("Input").Element("Content").Elements().Select(c => c.ToString()))
                }
            });
    }
}
Florian Schmidinger
  • 4,682
  • 2
  • 16
  • 28
  • Hi Florian, didnt realise we could use linq to xml. Actually I can work with the example you provided. thanks very much – user1131926 Mar 10 '15 at 12:26