# FSharp.Data XML match a string of XML with type

**URL:** <https://forums.fsharp.org/t/fsharp-data-xml-match-a-string-of-xml-with-type/1518>\
**Category:** General\
**Tags:** beginners\
**Created:** [October 31, 2020, 11:27pm UTC](https://forums.fsharp.org/t/fsharp-data-xml-match-a-string-of-xml-with-type/1518 "2020-10-31T23:27:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvanRainbolt](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ivanrainbolt/32/716_2.png) [@IvanRainbolt](https://forums.fsharp.org/u/IvanRainbolt)\
**Post date:** [October 31, 2020, 11:27pm UTC](https://forums.fsharp.org/t/fsharp-data-xml-match-a-string-of-xml-with-type/1518/1 "2020-10-31T23:27:46Z")

</div>

I am stuck. I get a response (how does not matter) that is an xml document represented as a string. The problem is that response can be of different XML schemas. I need to determine the schema type and THEN parse it to use it as I see it. I could cheat and just look for an identifier (string match) in the response (string xml).

But is there a way using the FSharp.Data XML stuff to do that match?

```
type ResponseType1 = FSharp.Data.XmlProvider<"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xSuccess xmlns:ns2="http://www.x.com"><Token>sample</Token></xSuccess>""">
type ResponseType2 = FSharp.Data.XmlProvider<"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?><aFault xmlns:ns2="http://www.x.com"><Message>sample</Message></aFault>""">

//send request
//for simplicity the response is a string in XML format
let rawResponseString = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xSuccess xmlns:ns2="http://www.x.com"><Token>abcdef</Token></xSuccess>"""

//let x2 = do something to rawResponseString here so I can match the type??

let eval x2 = match x2 with
            | :? ResponseType1 -> printfn "this response is a ResponseType1"
            | :? ResponseType2 -> printfn "this response is a ResponseType2"

let rt1 x = 
    let x' = ResponseType1.Parse(x)
    x'.Token

let rt2 x = 
    let x' = ResponseType2.Parse(x)
    x'.Message
```

---

<div class="post-metadata">

**Author:** ![amieres](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/amieres/32/687_2.png) [@amieres](https://forums.fsharp.org/u/amieres)\
**Post date:** [November 2, 2020, 10:59pm UTC](https://forums.fsharp.org/t/fsharp-data-xml-match-a-string-of-xml-with-type/1518/2 "2020-11-02T22:59:20Z")

</div>

You can always parse with any of the provider types and use `XElement` member to check the content.

For instance, you can do something like this:

```
type XmlResp = 
| XSuccess of Token : string
| AFault of Message: string

let loadXmlResp xml = 
    let resp = ResponseType1.Parse xml
    match resp.XElement.Name.LocalName with
    | "xSuccess" -> resp.Token |> XSuccess
    | _ -> (ResponseType2.Parse xml).Message |> AFault
```
