Reading XML attributes without knowing the element name
-
I am just learning XML and C# so I appoligize for any inconvience in advance. The program I am working on interfaces with another program that I did not write. I create a XML request for the program to do something and it responds with an XML response that tells me if it was succesful. The problem I have is parsing the response to find out if there are any errors. A basic response looks like this: The problem I have is that each of the elements could be different, as in this case, there is a VendorAddRs element and a CustomerAddRs element. I have been trying to use the XmlTextReader and XMLDoc to try and check to see if the statusCode is something other than 0. A lot of the information I have found assumes that you know what element you are looking for. I don't care what the element is (if it's a customerAdd or a VendorAddRs) but really what the statuscode is and what the statusmessage is the code is anything other than 0. I was thinking that I could just read through the xml and see if the node I'm at has any attributes, and if it does, check the first attibute to see if it starts with "statusCode" but that hasn't worked so far. If there is an article or a post that someone could point me to, I'd appreciate it, as I have not had any luck finding an article that addresses this. Thank you so much for your time.
-
I am just learning XML and C# so I appoligize for any inconvience in advance. The program I am working on interfaces with another program that I did not write. I create a XML request for the program to do something and it responds with an XML response that tells me if it was succesful. The problem I have is parsing the response to find out if there are any errors. A basic response looks like this: The problem I have is that each of the elements could be different, as in this case, there is a VendorAddRs element and a CustomerAddRs element. I have been trying to use the XmlTextReader and XMLDoc to try and check to see if the statusCode is something other than 0. A lot of the information I have found assumes that you know what element you are looking for. I don't care what the element is (if it's a customerAdd or a VendorAddRs) but really what the statuscode is and what the statusmessage is the code is anything other than 0. I was thinking that I could just read through the xml and see if the node I'm at has any attributes, and if it does, check the first attibute to see if it starts with "statusCode" but that hasn't worked so far. If there is an article or a post that someone could point me to, I'd appreciate it, as I have not had any luck finding an article that addresses this. Thank you so much for your time.
If you know the attribute name you could use XPath: XmlDocument myXMLDoc = new XmlDocument(); myXMLDoc.Load(responseStream); XmlNode myAttribute = myXMLDoc.SelectSingleNode("//@attributeIAmLookingFor"); This is a very good XPath tutorial.[^]
----- You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies If atheism is a religion, then not collecting stamps is a hobby. -- Unknown God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
-
If you know the attribute name you could use XPath: XmlDocument myXMLDoc = new XmlDocument(); myXMLDoc.Load(responseStream); XmlNode myAttribute = myXMLDoc.SelectSingleNode("//@attributeIAmLookingFor"); This is a very good XPath tutorial.[^]
----- You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies If atheism is a religion, then not collecting stamps is a hobby. -- Unknown God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
Wow! XPath seems really usefull for XML! Thank you very much for the article! It appears that this is what I need. Since there can be multiple elements in one document that have statusCodes, I would venture to say that I should use: XmlNodeList myNodeList = myXMLDoc.SelectNodes("//@attributeIAmLookingFor"); foreach(XmlNode n in myNodeList) { // Check the value of the node 'n' to see if it != "0" } I'm going to do some testing on this right now and then sit down and read up on XPath some more! Thanks again!
-
Wow! XPath seems really usefull for XML! Thank you very much for the article! It appears that this is what I need. Since there can be multiple elements in one document that have statusCodes, I would venture to say that I should use: XmlNodeList myNodeList = myXMLDoc.SelectNodes("//@attributeIAmLookingFor"); foreach(XmlNode n in myNodeList) { // Check the value of the node 'n' to see if it != "0" } I'm going to do some testing on this right now and then sit down and read up on XPath some more! Thanks again!
hpjchobbes wrote:
Since there can be multiple elements in one document that have statusCodes, I would venture to say that I should use: XmlNodeList myNodeList = myXMLDoc.SelectNodes("//@attributeIAmLookingFor"); foreach(XmlNode n in myNodeList) { // Check the value of the node 'n' to see if it != "0" }
There you go my friend! If your document uses XML namespaces, it can get hairy. In that case, you will have 2 choices: 1) Use the class XmlNamespaceManager in your calls to SelectNodes, if your XML document uses prefixes. 2) If it does not use prefixes, you will have to resort to fully-qualified XPath queries, like:
//@*[local-name()='attributeIAmLookingFor' and namespace-uri()='http://somenamespace.com']
This is an alternative XPath syntax. Also, from your example above, you could save some code: //@*[local-name='attributeIAmLookingFor' and namespace-uri()='http://somenamespace.com' and number()!=0] Have fun!
----- You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies If atheism is a religion, then not collecting stamps is a hobby. -- Unknown God is the only being who, to rule, does not need to exist. -- Charles Baudelaire