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));
Related
I'm trying to write a console app that will generate documentation pages based on the XML documentation file Visual Studio generates. So here's the problem, look at the XML generated by Visual Studio:
<?xml version="1.0"?>
<doc>
<assembly>
<name>Assembly-CSharp</name>
</assembly>
<members>
<member name="M:Utility.ScaleRange(System.Single,System.Single,System.Single,System.Single,System.Single)">
<summary>
Scales x from inA-inB to outA-outB
</summary>
<param name="_x">Value to scale</param>
<param name="_inA">Input range min</param>
<param name="_inB">Input range max</param>
<param name="_outA">Output range min</param>
<param name="_outB">Output range max</param>
<returns>Scaled value</returns>
</member>
</members>
</doc>
As you can see there's no way to know what type the method returns unless I write it in <returns> when commenting my code (which is something I don't want to do).
Is there a way to make VS output the type of the method? If not then I'm guessing I'm gonna have to use Reflection to help me out with this project.
Thanks in advance!
I have the following dummy fake sample:
<family>
<member> dad </member>
<member> mum </member>
<member> son </member>
<member> grandad<> </member>
</family>
I have been given a document to convert into XML but I have been unsuccessful so far in doing so. I have no control over how the document (html) given to me is created but I need to convert the document to xml; So that I can convert it using a stylesheet.
TidyManaged and HAP are no good to me at this stage in my workflow. Will explain more if people are interested knowing why.
In order for me to use HAP successfully, I need the above sample to look like the below:
<family>
<member> dad </member>
<member> mum </member>
<member> son </member>
<member> grandad<> </member>
</family>
My last approach before I give up on this problem would be, to read in my source html document, treat it as a plan text document and read it line by line.
I require someone to give me some regex that will successfully match the inner text of an element i.e:
<member> grandad<> </member>
Would give me the string:
"grandad<>"
If I can get this far, I should be able to convert the angle brackets into html key code equivalents. This should then pass as valid XML allowing me to load this into an XDocument class.
Then replace that result string back with this one:
<member> grandad<> </member>
When all special characters have been 'escaped' like this properly then I will be in a position to leverage the benefits of HTML Agility Pack (HAP) otherwise I will have to give up.
Thanks for reading.
The simplest Regular Expressions
var reg = new Regex(#"(?<=<(\w+)>)(.*)(?=</\1>)");
var input = "<member> grandad<Regexp is a bad tool because of <strong>this</strong>> </member>";
var output = reg.Match(input).Value;
Problem will be if your member tag contains any white spaces or attributes or more then one member tag will be in single line. So if you can provide ugliest example I'll change expression to adjust your input.
If you can process each document manually then you can use notepad++.
The reindent xml(TextFX->TextFX HTML Tools->Reindent xml> functionality will automatically impose the entities you want.
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.
Is it valid in XML-RPC to have an unbounded array of elements without having them inside of a array/data parent? From my limited experience with XML-RPC I have seen that arrays should be listed as such:
<member>
<name>Name</name>
<value>
<array>
<data>
<value>
<string>Red</string>
</value>
<value>
<string>Blue</string>
</value>
</data>
</array>
</value>
</member>
...with the parent Name having children strings Red and Blue. However, the 3rd party RPC service we are integrating with sends arrays of unbounded elements without having them inside of the array/data element, but inside a struct, e.g.
<member>
<name>Name</name>
<value>
<struct>
<member><name>Option0</name>
<value><string>Red</string>
</member>
<member><name>Option1</name>
<value><string>Blue</string>
</member>
</struct>
</value>
</member>
...With the values of Option1 and Option2 encapsulated inside of a struct.
The problem I am facing is that when designing the classes that will be serialized, I would have to design my class such as
private string Option0
private string Option1
...
...instead of:
private string[] Name
As I do not know the number of unbounded fields coming back in the structure, it seems the right way to accomplish the task would be to have an array of strings to enumerate through. However, there are no arrays in the resoponse XML, just structures with a dynamic number of fields. Because of that, I would have to list a large number of fields to conform to the structure, even though it's not really a structure, but an array. Is there something I am missing with the XML-RPC?
Yes it is quite valid XML-RPC struct. We also have such case and are using Cook Computings' XML-RPC.NET. It works perfectly. Check it, there is a special class there called XmlRpcStruct. You just need to use it in your XML-RPC method request or response.
I need to embed an entire well-formed xml document within another xml document. However, I would rather avoid CDATA (personal distaste) and also I would like to avoid the parser that will receive the whole document from wasting time parsing the embedded xml. The embedded xml could be quite significant, and I would like the code that will receive the whole file to treat the embedded xml as arbitrary data.
The idea that immediately came to mind is to encode the embedded xml in base64, or to zip it. Does this sound ok?
I'm coding in C# by the way.
You could convert the XML to a byte array, then convert it to binary64 format. That will allow you to nest it in an element, and not have to use CDATA.
The W3C-approved way of doing this is XInclude. There is an implementation for .Net at http://mvp-xml.sourceforge.net/xinclude/
Just a quick note, I have gone the base64 route and it works just fine but it does come with a stiff performance penalty, especially under heavy usage. We do this with document fragments upto 20MB and after base64 encoding they can take upwards of 65MB (with tags and data), even with zipping.
However, the bigger issue is that .NET base64 encoding can consume up-to 10x the memory when performing the encoding/decoding and can frequently cause OOM exceptions if done repeatedly and/or done on multiple threads.
Someone, on a similar question recommended ProtoBuf as an option, as well as Fast InfoSet as another option.
Depending on how you construct the XML, one way is to not care about it and let the framework handle it.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><helloworld></helloworld>");
string xml = "<how><are><you reply=\"i am fine\">really</you></are></how>";
doc.GetElementsByTagName("helloworld")[0].InnerText = xml;
The output will be something like a HTMLEncoded string:
<?xml version="1.0" encoding="utf-8"?>
<helloworld><how><are><you
reply="i am fine">really</you></are></how>
</helloworld>
I would encode it in your favorite way (e.g. base64 or HttpServerUtility::UrlEncode, ...) and then embed it.
If you don't need the xml declaration (first line of the document), just insert the root element (with all childs) into the tree of the other xml document as a child of an existing element. Use a different namespace to seperate the inserted elements.
It seems that serialization is the recommended method.
Can't you use XSLT for this? Perhaps using xsl:copy or xsl:copy-of? This is what XSLT is for.
I use Comments for this :
<!-- your xml text -->
[EDITED]
If the embedded xml with comments, replace it with a different syntax.
<?xml version="1.0" encoding="iso-8859-1" ?>
<xml>
<status code="0" msg="" cause="" />
<data>
<order type="07" user="none" attrib="..." >
<xmlembeded >
<!--
<?xml version="1.0" encoding="iso-8859-1" ?>
<xml>
<status ret="000 "/>
<data>
<allxml_here />
<!** embedeb comments **>
</data>
<xml>
-->
</xmlembeded >
</order>
<context sessionid="12345678" scriptname="/from/..." attrib="..." />
</data>
</xml>