0

This is my xml string

 string fromHeader= "<a:From><a:Address>http://ex1.example.org/</a:Address></a:From>";

I want to load it into an XElement, but doing XElement.Parse(fromHeader) gives me an error due to the 'a' prefixes. I tried the 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, but seriously, do i need 4 lines of code to do this! What is a quickest / shortest way of getting my XElement?

joedotnot
  • 4,810
  • 8
  • 59
  • 91

1 Answers1

0

I found out the above 4 lines are 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 the NS into the '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);
joedotnot
  • 4,810
  • 8
  • 59
  • 91