xml parsing - Convert Xml String with node prefixes to XElement -
this xml string
string fromheader= "<a:from><a:address>http://ex1.example.org/</a:address></a:from>"; i want load xelement, doing xelement.parse(fromheader) gives me error due 'a' prefixes. tried following:
xnamespace xnsa = "http://www.w3.org/2005/08/addressing"; string dummyroot = "<root xmlns:a=\"{0}\">{1}</root>"; var fromxmlstr = string.format(dummyroot, xnsa, fromheader); xelement xfrom = xelement.parse(fromxmlstr).elements().first(); which works, seriously, need 4 lines of code this! quickest / shortest way of getting xelement?
i found out above 4 lines equivalent to
xnamespace xnsa = "http://www.w3.org/2005/08/addressing"; xelement xfrom = new xelement(xnsa + "from", new xelement(xnsa + "address", "http://ex1.example.org/")); or alternatively move ns 'from' element before parsing.
var fromstr = "<a:from xmlns:a=\"http://www.w3.org/2005/08/addressing\"><a:address>http://ex1.example.org/</a:address></a:from>"; xelement xfrom = xelement.parse(fromstr);
Comments
Post a Comment