i want to SelectSingleNode with index # since i have few elements with the same path.
xDoc.DocumentElement.SelectSingleNode(xPath).InnerText = xValue.ToString();
When xPath is the following string:
"/Parameter [#tag='tool_od']/Value/ValueSeries/Value[Index=1]/value"
or
"/Parameter [#tag='tool_od']/Value/ValueSeries/Value[1]/value"
or
"/Parameter [#tag='tool_od']/Value/ValueSeries/Value[#Index=1]/value"
all of those options gives me an error:
"Object reference not set to an instance of an object."
this is the part of the xml:
i want to be able to access each of childs with selectsinglenode.
<ValueSeries>
<Value>
<value>25</value>
</Value>
<Value>
<value>999012.0</value>
</Value>
<Value>
<value>999012.0</value>
</Value>
</ValueSeries>
if i will remove the index part the path will work fine but it will only access the first element and not the others.
It's hard to be sure what exactly your problem is without being able to see your input xml.
Note that you don't need to use xDoc.DocumentElement as your xpath is referring to the root node anyway (/), so you can just do xDoc.SelectSingleNode(....
If you are looking for the first "Value" element of "ValueSeries" your second xpath looks correct (does a value contain a value?), but it depends on what your xml looks like.
The "Object reference" error is due to the fact that SelectSingleNode is returning null (as your xpath is not found), and you are trying to set the property InnerText.
my error was due of using index "0", the first index is 1.
Related
The problem is when a try to set an XmlAttribute value with the '>' char. It escape with the value '>', but '>' es valid because it is into an attribute value. Example:
var element = xmlDoc.CreateElement("Studient");
element.SetAttribute("Year", ">3");
xmlDoc.DocumentElement.AppendChild(element);
xmlDoc.Save(csprojPath);
In this case it produce <Studient Year=">3" xmlns="" />. How can i get <Studient Year=">3" xmlns="" /> ?
As far as I know, There is no way to create the data like ">3" in attribute value. If you open created XML in reader it will display as ">3" only. If you are parsing XML in code , you can easily convert to intended meaning.
If you can use Section value instead of Attribute Value for Student Year, you can use XmlCDataSection by CreateCDataSection (for example: https://msdn.microsoft.com/en-us/library/vstudio/system.xml.xmldocument.createcdatasection(v=vs.100).aspx)
I'm using XDocument and I need to parse my XML file to retrieve all attribute with the same name event if its node's name is different from the other.
For example, for this XML :
<document>
<person name='jame'/>
<animals>
<dog name='robert'/>
</animals>
</document>
I want to retrieve all attributes named 'name'.
Can I do that with one request XPath or do I need to parse every node to find thos attributes ?
Thanks for your help !
The XPath expression
//#name
will select all attributes called name, regardless of where they appear.
By the way, 'parsing' is something that happens to the XML document before XPath ever enters the picture. So when you say "do I need to parse every node", I think this isn't really what you mean. The entire document is typically already parsed before you run an XPath query. However, I'm not sure what you do mean instead of 'parse'. Probably something like "do I need to visit every element" to find those attributes? In which case the answer is no, unless in some vague implementation-dependent sense that doesn't make any difference to you.
I am attempting to parse some XML data using XPath queries in C#. But my query is not successfully finding the element I am looking for (it finds nothing).
Whats wrong with my XPath query? Is my syntax for following-sibling incorrect or something? How can I edit my XPath to find the correct value element?
<attributes>
<real>
<name>cover rl</name>
<value>89.87414122</value>
</real>
<real>
<name>pit depth</name>
<value>2.35620671</value> <!-- This is the value I need -->
</real>
<attributes>
My XPath query that fails:
ns:attributes/real/name[text() = 'pit depth']/following-sibling::value
You're close. Mostly get rid of the spurious ns: namespace prefix. Also note that your sample input XML should end with a closing </attributes> element rather than another opening <attributes> element
So, this XPath:
/attributes/real/name[. = 'pit depth']/following-sibling::value
Will yield:
<value>2.35620671</value>
per your request.
If you only want the contents of the value element:
/attributes/real/name[. = 'pit depth']/following-sibling::value/text()
Will yield:
2.35620671
I'm trying to call an xml-rpc web service method that takes 1 parameter (an array of values) key and leads.
Key must be named 'key' and must have a value of type string.
Leads is an xml document containing the leads data.This must be packaged as a binary object. This value must be named leads and must be of type base64.
Alright so the SINGLE parameter for this method call in python is:
r = proxy.leads({'key': key, 'leads': doc})
My first question is how can I do this in c#? The closest thing .net has to that is a dictionary object which won't work for this.
Secondly, how do I make the xml doc a binary object of type base64? Is that the same as converting a byte[] array to base64 string? Like this:
Convert.ToBase64String(byteArray)
Here is what the request should look like:
<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall>
<methodName>leads</methodName>
<params>
<param>
<value>
<struct>
<member>
<name>key</name>
<value>
<string>XXXXXXXXXXX</string>
</value>
</member>
<member>
<name>leads</name>
<value>
<base64>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPGxlYWRzPgogICA8bGVhZD4K
ICAgICAgPGlkPjM5OTk3PC9pZD4KICAgICAgPEZpcnN0TmFtZT5Cb2IgSmltPC9GaXJzdE5hbWU+
CiAgICAgIDxMYXN0TmFtZT5TbWl0aDwvTGFzdE5hbWU+CiAgICAgIDxBZGRyZXNzPjEyMzQgV2Vz
:
:
ICAgICA8UmVjZWl2ZUFkZGxJbmZvPlllczwvUmVjZWl2ZUFkZGxJbmZvPgogICAgICA8bG9wX3dj
X3N0YXR1cz5ObzwvbG9wX3djX3N0YXR1cz4KICAgPC9sZWFkPgo8L2xlYWRzPg==
</base64>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodCall>
I'm completely stuck on this problem. Any help would be much appreciated.
Check this out http://codinghints.blogspot.com/2010/03/xml-rpc-calls-with-c.html to see how one can manually call the service. There are probably libraries to do it in nice way...
How you specify parameters depends on what approach you find to construct the request. In case of manually constructing request (I'd recommend XDocument to build XML, not String.Format, but String.Format may be ok in very simple cases like your example) you would just put values into right places in boilerplate XML...
Yes byte array to base64 is Convert.ToBase64String(byteArray).
Something like following could be enough (but please try use proper ways to construct XML for non-one-time-use code):
String.Format("<?xml versi... <name>key</name><value><string>{0}</string>...",
key, Convert.ToBase64String(byteArray));
I am trying to select all nodes with attribute equal to something, I got the error in title.
My Xpath string looks like //#[id=****], anyone know what's wrong?
Your XPath expression probably should be:
//*[#id='something']
Which means match all elements whose id attributes are equal to something, anywhere in the document.
EDIT: If you want the id attribute nodes themselves and not their parent elements, you can use:
//*[#id='something']/#id
Or even better, as #Dimitre Novatchev suggested:
//#id[. = 'something']
I am trying to select all nodes with
attribute equal to something, I got
the error in title.
My Xpath string looks like
//#[id=****], anyone know what's
wrong?
A lot of issues with this expression:
.1. //#[some-condition] The predicate can only be applied to selected nodes, but //# doesn't select any node. # is an abbreviation for attribute:: and this is an unfinished node-test. It is missing the node-type or node-name here.
What would be correct is: //#*[some-condition] or //#attrName[some-condition]
.2. id=**** is syntactically invalid, unless ** is a valid XPath expression itself. My guess is that you want to get all attributes with value equal to some known, literal value. In any such case the syntax to use is id='someLiteral -- do note the single quotes (they can also be double quotes) surrounding the literal value.
Solution:
//*[#id='something']
This selects all elements in the XML document that have attribute id with value 'something'.
//#id[. = 'something']
This selects all attributes named id in the XML document, whose value is 'something'.
//#*[. = 'something']
This selects all attributes in the XML document (regardless of their name), whose value is 'something'.