1

My xml looks like this:

<document>
    <body>
        <p>
            <text>asdasdasdasd</text>
        </p>
        <text>  </text>
        <p>
            <text>Bl abloa blasdasdasd</text>
        </p>
    </body>     
</document>

So what i want to do is to parse body element as string.

    [XmlRoot(ElementName = "document")]
    public class Document
    {      
        [XmlElement(ElementName = "body")]
        public string Body { get; set; }   
    }   

I've tried [XmlText] and different attributes on like this [XmlText(Type = typeof(string))]

I'm trying to do this directly as a parameter in my controller method:

    [AllowAnonymous]
    [HttpPost]
    [Route("")]
    public async Task SearchResult([FromBody] SearchResultDataContract searchResult)
    {
        try

Were SearchResultDataContract is the document object.

But I haven't managed to find any solutions.

Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78

2 Answers2

2
[XmlRoot(ElementName = "document")]
public class Document
{
    [XmlElement("body")]
    public XmlElement Body { get; set; }
}

should work, as long as we're talking about xhtml, not html. You can't deal with string directly, AFAIK - the encoder won't trust you that your xml will always be well-formed - but it trusts XmlElement. You could always add something that shims between the two, if needed.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

I managed to solve this by creating my own extension for the deserialization using this: Deserialize element value as string although it contains mixed content

Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78