I need to generate XML on MonoTouch application ? How i can create like this,
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
...
</root>
I need to post the XML to web service as string format. And again I want to parse the response data. Response data also XML format string. I done it in j2me applications. But I don't know how to do this on MonoTouch? Anyone tell me How to do this?
My favorite API these days is the System.Xml.Linq API, look at the documentation and samples here:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx
For what you want, this is very easy:
var s = new XDocument () {
new XElement ("root")
};
Console.WriteLine ("The XML is: {0}", s.ToString ());
You can create the XML by using the XmlWriter class.
The easiest way to parse XML would be to use LINQ.
Related
So I'm in a bit of a cross road here trying to consume the data in files that from my understanding (after reading up a bit) seems to be multipart soap requests (with the xml object as an attachment?).
So I've been given the task to work with xml files that look something like this:
--MIME264440613829.7322990959788848043325807015
<SOAP-ENV:Envelope>
<SOAP-ENV:Header>
...
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<eb:Manifest eb:version="2.0">
<eb:Reference xlink:href="cid:payload-1" xlink:role="aop:ROOT"/>
</eb:Manifest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
--MIME264440613829.7322990959788848043325807015
Content-ID: payload-1
<?xml version="1.0" encoding="UTF-8"?>
<aop:ROOT>
...
</aop:ROOT>
--MIME264440613829.7322990959788848043325807015--
And what i need to do is to extract the XML in what seems to be the second part (or the attachment as i think people call it) from the multipart object.
My first thought was to just use some string operations with for example regex to extract the xml object, but surely there must be a better way. Also I'm currently just testing this out in a c# project.
Ok, So after contacting one of my colleagues we managed to find a solution:
So this is what we did
Using HttpMultiPartParser we could extract the xml content from the file by the code below:
using HttpMultipartParser;
using (var streamReader = new StreamReader(#"multipartFile.xml"))
{
var temp = MultipartFormDataParser.Parse(streamReader.BaseStream);
foreach (var filepart in temp.Files)
{
using (var fpSr = new StreamReader(filepart.Data))
{
var name = filepart.Name;
var contentType = filepart.ContentType;
var content = fpSr.ReadToEnd();
}
}
}
I'm working on a tool for validating XML files grabbed from a mainframe. For reasons beyond my control every XML file is encoded in ISO 8859-1.
<?xml version="1.0" encoding="ISO 8859-1"?>
My C# application utilizes the System.XML library to parse the XML and eventually a string of a message contained within one of the child nodes.
If I manually remove the XML encoding line it works just fine. But i'd like to find a solution that doesn't require manual intervention. Are there any elegant approaches to solving this? Thanks in advance.
The exception that is thrown reads as:
System.Xml.XmlException' occurred in System.Xml.dll. System does not support 'ISO 8859-1' encoding. Line 1, position 31
My code is
XMLDocument xmlDoc = new XMLDocument();
xmlDoc.Load(//fileLocation);
As Jeroen pointed out in a comment, the encoding should be:
<?xml version="1.0" encoding="ISO-8859-1"?>
not:
<?xml version="1.0" encoding="ISO 8859-1"?>
(missing dash -).
You can use a StreamReader with an explicit encoding to read the file anyway:
using (var reader = new StreamReader("//fileLocation", Encoding.GetEncoding("ISO-8859-1")))
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
// ...
}
(from answer by competent_tech in other thread I linked in an earlier comment).
If you do not want the using statement, I guess you can do:
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(File.ReadAllText("//fileLocation", Encoding.GetEncoding("ISO-8859-1")));
Instead of XmlDocument, you can use the XDocument class in the namespace System.Xml.Linq if you refer the assembly System.Xml.Linq.dll (since .NET 3.5). It has static methods like Load(Stream) and Parse(string) which you can use as above.
This is my code:
var objectText = XmlReader.Create(requestedURL);
XmlSerializer mySerializer = new XmlSerializer(typeof(InstagramItems));
var instagramItems = (InstagramItems)mySerializer.Deserialize(objectText);
but seems it can't work with RSS (which are "XML more or less"):
Server Error - <rss xmlns=''> was not expected.
How can I do it? I believe there are .NET library without using 3rd part plugins.
Part of the RSS:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>Photos tagged as "example" on Instagram</title>
<link>http://instagram.com</link>
<description>Photos tagged as "example" on Instagram</description>
<atom:link href="http://instagram.com/tags/example/feed/recent.rss" rel="self" />
</channel>
Step 1: Download the RSS XSD : http://www.thearchitect.co.uk/schemas/rss-2_0.xsd
Step 2: Use xsd.exe to generate an RSS type based on the schema
Step 3: If objectText is only an RSS document, then just substitute your newly created RSS type for InstagramITems in the above code.
Step 3a: If the object text is mixed Instagram and RSS code, then use the DataContractSerializer and pre-register both Instagram and RSS types with the DataContractSerializer before attempting to deserialize
XmlSerializer works with specific xml format, if you suply any other format other then which it expects the metioned exception will be throwned.
You can parse the xml manualy and create InstagramItems from the parsed xml, I would recommed using linq to xml here is an example http://social.msdn.microsoft.com/Forums/vstudio/en-US/e38e69ac-d325-4cc4-bdf7-bc940e19e63f/read-xml-and-create-objects-using-linq
I have the following XML obtain via a SOAP call.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetValueListForFieldResponse xmlns="http://URLHere/webservices/">
<GetValueListForFieldResult>
<SelectDef> <Id>1736</Id> <SelectName>Values List</SelectName> <GlobalFlag>False</GlobalFlag> <Sort>1</Sort> <SelectDefValues> <SelectDefValue> <guid>aaf6f3a7-6a74-4187-b4e7-3a9355b16796</guid> <Id>14692</Id> <Name>Open</Name> <Description></Description> <Color>#000000</Color> <DefaultFlag>False</DefaultFlag> <SortOrder>1</SortOrder> <select_id>1736</select_id> </SelectDefValue> <SelectDefValue> <guid>f5082b54-d799-4fdc-80c1-0e232b360057</guid> <Id>14693</Id> <Name>Closed</Name> <Description></Description> <Color>#000000</Color> <DefaultFlag>False</DefaultFlag> <SortOrder>0</SortOrder> <select_id>1736</select_id> </SelectDefValue> <SelectDefValue> <guid>94e29e78-2ab3-463f-bbb6-ab7f36003c7f</guid> <Id>14780</Id> <Name>Past Due</Name> <Description></Description> <Color>#000000</Color> <DefaultFlag>False</DefaultFlag> <SortOrder>2</SortOrder> <select_id>1736</select_id> </SelectDefValue> </SelectDefValues> </SelectDef>
</GetValueListForFieldResult>
</GetValueListForFieldResponse>
</soap:Body>
Is there a way to convert the data in the GetValueForFieldResult node to actual XML so I can parse the data?
Below is how I'm making the SOAP call and storing the XML. I'm learning C# and if below is a complete mess my appologies.
HttpWebRequest reqVl = (HttpWebRequest)WebRequest.Create(serverURL + "/ws/Field.asmx");
reqVl.Headers.Add("SOAPAction", "http://URL/webservices/GetValueListForField");
reqVl.ContentType = "text/xml;charset=\"utf-8\"";
reqVl.Accept = "text/xml";
reqVl.Method = "POST";
using (Stream stm = reqVl.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(VLsoap);
}
}
WebResponse responseVL = reqVl.GetResponse();
Stream responseStreamVL = responseVL.GetResponseStream();
XmlReader rdrVL = XmlReader.Create(responseStreamVL);
XmlDocument vls = new XmlDocument();
vls.Load(rdrVL);
Here is some code to achieve what you want - however, please read the text afterwards for an explanation of why this may not be the best way to get what you want.
Tested as working with your message and .Net 4.
Assuming vls contains your SOAP message as XML, we split the problem into two halves; parsing the soap message, and extracting and decoding the contents of the GetValueListForFieldResult node into a string that can be loaded into another XmlDocument
Part I - getting the contents of the GetValueListForFieldResult node
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(vls.NameTable);
namespaceManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
namespaceManager.AddNamespace("default", "http://URLHere/webservices/");
XmlNode payLoadNode =
vls.SelectSingleNode("/soap:Envelope/soap:Body/default:GetValueListForFieldResponse/default:GetValueListForFieldResult", namespaceManager);
string encodedXml = payLoadNode.InnerText;
Part II - getting the encoded string into an Xml Document
It is at this point that we have the encoded string. We have a couple of choices for decoding this HTML; as I'm using .Net 4 I've gone for the simplest:
string decodedXml = WebUtility.HtmlDecode(encodedXml);
XmlDocument payloadDocument = new XmlDocument();
payloadDocument.LoadXml(decodedXml);
If you are using .Net 3.5 then you'll have to consider adding a reference to System.Web and using HttpUtility.HtmlDecode instead to decode the string.
Parsing your message above gives me the result:
<SelectDef>
<Id>1736</Id>
<SelectName>Values List</SelectName>
<GlobalFlag>False</GlobalFlag>
<Sort>1</Sort>
<SelectDefValues>
<SelectDefValue>
<guid>aaf6f3a7-6a74-4187-b4e7-3a9355b16796</guid>
<Id>14692</Id>
<Name>Open</Name>
<Description></Description>
<Color>#000000</Color>
<DefaultFlag>False</DefaultFlag>
<SortOrder>1</SortOrder>
<select_id>1736</select_id>
</SelectDefValue>
<SelectDefValue>
<guid>f5082b54-d799-4fdc-80c1-0e232b360057</guid>
<Id>14693</Id>
<Name>Closed</Name>
<Description></Description>
<Color>#000000</Color>
<DefaultFlag>False</DefaultFlag>
<SortOrder>0</SortOrder>
<select_id>1736</select_id>
</SelectDefValue>
<SelectDefValue>
<guid>94e29e78-2ab3-463f-bbb6-ab7f36003c7f</guid>
<Id>14780</Id>
<Name>Past Due</Name>
<Description></Description>
<Color>#000000</Color>
<DefaultFlag>False</DefaultFlag>
<SortOrder>2</SortOrder>
<select_id>1736</select_id>
</SelectDefValue>
</SelectDefValues>
</SelectDef>
The Alternative
The reason you may not wish to do it this way is because the response you are receiving has been wrapped using SOAP; you may therefore wish to try and consume the service you are connecting to as a web service and generate a proxy library; this will encapsulate all of the code you see above, as well as the code you have written, in a proxy that may allow you to retrieve the values you want in a more type safe and less fragile manner. Support for this is built into .Net.
Further, as the URL you are connecting to terminates in ASMX, it tells you that this is most likely a native .Net web service so wiring your client up to it should be simple.
The MSDN topic "How to add a Reference to a Web Service" should help you in generating the proxy and avoiding all of the code above.
I have a xml structure stored in XDocument.
I want to present as html document (or something similar) , main idea that a web browser will be able to present it .
Does XSLT will right technology here ?
Is there some examples for how to do so ?
Thansk for help.
Yes, XSLT is good for this. I recently had to do this using the following code:
var xslt = new XslCompiledTransform(true);
xslt.Load(styleSheetFile, XsltSettings.TrustedXslt, new XmlUrlResolver());
xslt.Transform(xmlFile, outputFile);
You can use XSLT or LinqToXML. Many examples out there but you can start # http://msdn.microsoft.com/en-us/library/bb387098.aspx