Sonamor Sonamor - 1 year ago 63
C# Question

Issue with Namespace on XML Parsing c#

I am trying to read the following XML with C#

<GetAssetWarrantyResponse xmlns="" xmlns:s="">
<GetAssetWarrantyResult xmlns:i="" xmlns:a="">
<a:Faults />
<a:AssetParts i:nil="true" />
<a:ParentServiceTag i:nil="true" />
<a:ServiceLevelDescription>Collect and Return Initial with Dates</a:ServiceLevelDescription>
<a:ServiceProvider i:nil="true" />

I think I have an issue with the namespace and whatever I've tried so far didn't work.

I've tried so far

XNamespace aw = "a"
XNamespace aw = "a:"
XNamespace aw = ""

and some other than I can't recall at the moment

private void btDellWaranty_Click(object sender, EventArgs e)
string serviceTag = tbDellServiceTag.Text;
string uri=";
XDocument Doc = XDocument.Load(uri);
XNamespace aw = "a";
string result = doc.from u in Doc.Descendants(aw + "DellAsset") select (string)u.Element(aw + "MachineDescription").Value;

XElement result = Doc.Root.Element("GetAssetWarrantyResponse");


Any ideas?

Notes: I pass the
correctly to the
so that's not the issue.

Answer Source

The namespace for a prefix (e.g. a:DellAsset has the prefix a) is denoted by the xmlns:a="..." of the element or the first ancestor element that contains that namespace declaration - GetAssetWarrantyResult in this case.

So, your namespace is:

XNamespace aw = ""