I have a string like
new_responseString ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<lab:lab xmlns:udf=\"http://ghjhjhj.com/ri/userdefined\" xmlns:ri=\"http://kjkj.com/ri\" xmlns:lab=\"http://iuiuu.com/ri/lab\" uri=\"https://hhjjhjhj.org/api/v2/labs/1\">\n <name>Administrative Lab</name>\n <billing-address>\n <street></street>\n <city></city>\n <state></state>\n <country></country>\n <postalCode></postalCode>\n <institution></institution>\n <department></department>\n </billing-address>\n <shipping-address>\n <street></street>\n <city></city>\n <state></state>\n <country></country>\n <postalCode></postalCode>\n <institution></institution>\n <department></department>\n </shipping-address>\n <udf:field type=\"String\" name=\"Account ID\">adm</udf:field>\n <website></website>\n</lab:lab>\n"
In order to just extract the value adm ie any value between the tag <udf></udf>
var new_response = new_client.GetAsync(new_Uri).Result;
string new_responseString = new_response.Content.ReadAsStringAsync().Result;
var new_doc = XDocument.Parse(new_responseString);
var udf = "http://ghjhjhj.com/ri/userdefined";
var udfValue = doc.Descendants(udf + "field").FirstOrDefault(field => field.Attribute("name").Value.Equals("Account ID")).Value;
return udfValue;
But this throws an exception
System.Xml.XmlException: The ':' character, hexadecimal value 0x3A, cannot be included in a name
I have one more question instead of reading the response as string like ReadAsStringAsync()
in string new_responseString = new_response.Content.ReadAsStringAsync().Result
can this be retrieved in the original format the API returns the response.
The correct way to use a namespace is with XNamespace
:
XNamespace ns = "http://ghjhjhj.com/ri/userdefined";
var udfValue = new_doc.Descendants(ns + "field").FirstOrDefault(field => field.Attribute("name").Value.Equals("Account ID")).Value;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.