Create XML doc with Linq to XML - asp.net

I have an ASP .Net site where i am attempting to create a page to allow the user to create an XML document. New with XML so I'm not sure if im missing something here but all i am trying to do is create an XML document using Linq to XML.
I saw this example as part of my research
Dim xmlDoc As XDocument = _
<?xml version="1.0" encoding="utf-16"?>
<!-- This is comment by you -->
<Employees>
<Employee id="EMP001">
<name>Wriju</name>
<![CDATA[ ~~~~~~~...~~~~~~~~]]>
</Employee>
</Employees>
xmlDoc.Save("somepath\somefile.xml")
but i just dont seem to be able to get it working in VS 2010 or VS 2013. As soon as i enter <?xml and enter to the next line i just get errors that the syntax is incorrect. Reading further i have seen some C# examples but they dont seem to be "readable" in VB .Net.
I've tried using online converters but again no luck in that either. Could anyone guide me or point me to the right direction to create an XML document with elements etc using Linq to XML? I think i can use XDocument but i'm a little confused if that would allow me to use Linq to XML or if this is an old concept.
Thanks

Use this
Dim xmlDoc As XDocument = <?xml version="1.0" encoding="utf-16"?>
<!-- This is comment by you -->
<Employees>
<Employee id="EMP001">
<name>Wriju</name>
<![CDATA[ ~~~~~~~...~~~~~~~~]]>
</Employee>
</Employees>
'it assume that /youfolder is in the root and it has read and write permission
xmlDoc.Save(Server.MapPath("/yourfolder/somefile.xml"))

Related

Saxon HE 9.7 XQuery results and existing document

I am new to Saxon.
In my java application, I have a requirement that I need to XQuery an existing dom4j document. The XQuery is to order few elements in an descending order by serialNo:
<?xml version="1.0" encoding="UTF-8"?>
<dataOfBooks:DataOfBooks xmlns:dataOfBooks="DataOfBooks">
<Id>ID123</Id>
<books>
<book>
<name>ccc</name>
<serialNo>77</serialNo>
</book>
<book>
<name>aaa</name>
<serialNo>99</serialNo>
</book>
</books>
</dataOfBooks:DataOfBooks>
Once I get the XQuery results, I need to add those back to the above existing document. I tried using net.sf.saxon.s9api. I was able to get the XQuery results back as below:
<?xml version="1.0" encoding="UTF-8"?>
<result:sequence
xmlns:result="http://saxon.sf.net/xquery-results"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<result:element>
<book xmlns:data="dataOfBooks">
<name>aaa</name>
<serialNo>99</serialNo>
</book>
<book xmlns:data="dataOfBooks">
<name>aaa</name>
<serialNo>77</serialNo>
</book>
</result:element>
</result:sequence>
But I have two issues. 1) the result has namespaces and extra stuff that I do not want. 2) It is not very clear to me as which Saxon API to use to add the XQuery results to the existing document. So that the resultant document looks as:
<?xml version="1.0" encoding="UTF-8"?>
<dataOfBooks:DataOfBooks xmlns:dataOfBooks="DataOfBooks">
<Id>ID123</Id>
<books>
<book>
<name>aaa</name>
<serialNo>99</serialNo>
</book>
<book>
<name>ccc</name>
<serialNo>77</serialNo>
</book>
</books>
</dataOfBooks:DataOfBooks>
One more question - I tried using dynamicContext and treeinfo classes since I though the usage of treeinfo API might be more optimal, but no luck. If you think, usage of TreeInfo API is efficient, I really appreciate a code example for my requirement. Your help is much appreciated.
Thanks in advance for your time and interest.
The fact that your XQuery code is producing unwanted namespaces is because your query is wrong, but we can't tell you how it is wrong unless you show us the code.
The result:sequence in your output suggests that you have somehow contrived to ask for output in "wrapped" format, which suggests some kind of misuse of Saxon APIs. Again, without seeing your code, we can't tell you exactly what you have done wrong.
To make small changes to an existing document, leaving the rest unchanged, I would normally recommend XSLT over XQuery. In XSLT 3.0, you can sort the books by name using the following stylesheet:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="books">
<xsl:copy>
<xsl:perform-sort select="book">
<xsl:sort select="name"/>
</xsl:perform-sort>
</xsl:copy>
</xsl:template>
</xsl:transform>
In both XQuery and XSLT, the result of your query/transformation is a new document, which you can use in place of the original. If you want to make in-situ updates to an existing document, you can do this using XQuery Update; however Saxon does not support XQuery Update against documents in DOM4J format.
Saxon does allow you to capture the result of a query or transformation as a DOM4J Document, and you could use DOM4J APIs to graft this document (or rather, its outermost element) back into the original DOM4J document.
Later
You have now provided your code (you should have provided it as an edit to the original question, not as an answer).
I guess your DOMWriter is the DOM4J class of that name, which like much of DOM4J is rather badly documented. But I think it is copying the DOM4J tree to a DOM tree, which you definitely don't want to do. If you really want to copy the tree to make it convenient for Saxon, you should copy it to a Saxon tree, but for this use case it's best to leave it in DOM4J form. Use
DocumentBuilder builder = processor.newDocumentBuilder();
XdmNode inDoc = builder.wrap(dom4jdoc);
When you run your query, the resulting XdmValue will now be a sequence of XdmNode objects, each of which is a wrapper around a DOM4J Element node. These element nodes are still attached to the original DOM4J tree, and they still have their original namespaces. There is no need to serialize the result to lexical XML.
You can copy the result to a List value by writing
List<Element> sortedNodes = new ArrayList<Element>();
for (XdmItem item : result) {
sortedNodes.add(((Element)((XdmNode)item).getExternalNode()));
}
and then (if I read the DOM4J documentation correctly) you can replace the content of the containing books element with
Element books = (Element)sortedNodes.get(0).getParent();
List booksContent = books.elements();
booksContent.clear();
booksContent.addAll(sortedBooks);

Populating an RSS feed with our database content?

I have a question about creation of an RSS feed.
I have read a few different bits of information on creation of an RSS feed and this, on the whole doesn't appear too difficult.
What I'm struggling to understand though is how to automatically populate the RSS feed xml file?
Here is our basic .xml file...
<?xml version='1.0' encoding='utf-8'?>
<rss version='2.0'>
<channel>
<title>Channel Title</title>
<description>Description of Channel</description>
<link>http://www.test.com</link>
<item>
<title>Item Title</title>
<description>Updated on: 5/20/2012</description>
<link>http://www.test.com/itempage.asp?itemID=1</link>
</item>
</channel>
</rss>
We can, obviously, manually enter each item but I want this to automatically select the latest items from our database. In this case, news items.
you can use the Msxml2.DOMDocument.6.0 object. have a look here for MSXML DOM Reference.
with that object you can create xml nodes and then save the xml document...
example:
set xml = server.createobject("Msxml2.DOMDocument.6.0")
set rssNode = xml.createElement("rss")
rssNode.appendChild(xml.createTextNode(""))
rssNode.setAttribute "version", "2.0"

insert new item in sharepoint list from flex

i m trying to insert new item on sharepoint list from flex using sharepoint-as3-connector (http://code.google.com/p/sharepoint-as3-connector). but i m getting following error.
Response XML:<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>
<UpdateListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<UpdateListItemsResult>
<Results>
<Result ID="1,UpdateList.NEW">
<ErrorCode>0x8102000a</ErrorCode>
<ErrorText>Invalid URL Parameter
The URL provided contains an invalid Command or Value. Please check the URL again.
below is the header made in soap URL.
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<listName>
TestList
</listName>
<updates>
<Batch OnError="Continue">
<Method ID="1" Cmd="UpdateList.NEW">
<Field Name="ows_LinkTitle">
222222
</Field>
</Method>
</Batch>
</updates>
</UpdateListItems>
please help!
The error message is spot on: you are specifying an invalid command in the Cmd attribute of the Method element.
Per the MSDN article for the Method element, valid values for Cmd are:
Delete
New
Update
I have no experience with the "sharepoint-as3-connector", and I suppose it's there to make your life easier. But it's probably worth reviewing the MSDN documentation for the Lists web service (and specifically the UpdateListItems method) so you know what SharePoint is expecting. SharePoint is a delicate flower that cannot be handled harshly; you must know exactly what it wants to keep it happy.
A good walkthrough on MSDN: How to: Update List Items.

WebService remove <xml> tag from my XmlDocument

I use a WebService to transform and XmlDocument into a PDF.
The XmlDocument I send to the Web service looks like this.
<?xml version="1.0" encoding="utf-16" ?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
...
</fo:root>
I had a problem so I went in debug mod to find out that when the XmlDocument object is transferred from my asp website itself to the Web Service which works on .NET 1.1 sees his
xml tag. Is there a reason why this tag is removed? Could it be caused by the SOAP response?
Is there a way around other than manually add the tag back in the document?
Edit
To answer John's question, yes I mean the processing instruction. It just goes off and I was wondering why because the library I use to convert doesn't work without it. If I manually add it, it works fine but I just wanted to know why it disappear.
Edit 2
Even if it isn't a tag, the library that requires the XmlDocument just doesn't work without it that's why I need it. Other than that, the rest of the document is processed correctly. The generated Reference.cs from the Web Reference looked like this for the called method :
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GeneratePdfFromXml", RequestNamespace="http://tempuri.org", ResponseNamespace="http://tempuri.org", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")]
public byte[] GeneratePdfFromXml(XmlNode FormattedObjectXml) {
object[] results = this.Invoke("GeneratePdfFromXml", new object[] {
FormattedObjectXml});
return ((byte[])(results[0]));
}
It is the same issue as another problem I had, in which the XmlDocument are referenced as XmlNode since the SOAP response is a XmlDocument itself.
I just changed this to a string ; MyXmlDocument.OuterXml;
That way, everything is kept and no problem.
It is most likely an encoding issue. The XML Declaration is claiming the document is in UTF-16, which is two bytes per character. The other library probably is assuming, in its absence, some other encoding.
You will never get an XML Declaration or processing instruction passed to via an XmlNode, XmlElement or XmlDocument parameter to an ASMX service. The reason is obvious if you think about it. The SOAP Request would be something like:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" >
<SOAP-ENV:Body>
<parameter>
<?xml version="1.0" encoding="utf-16" ?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
</fo:root>
</parameter>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But an XML declaration can only appear at the very beginning of the document, so this is invalid.
The solution, as you found, is to send this XML as a string. Make your parameter type string, and either use XmlNode.OuterXml.

ASP.NET w-s ... consuming from Excel?

We have a web-service within an existing ASP.NET website which works fine when accessed via other ASP.NET bits. I'd like to get Excel 2007 and 2003 to call the w-s and refresh part of a worksheet with the results using VBA.
Ideally I'd like a vanilla version of Excel to be able to do this (ie without the client having to install extra bits).
As a starter (in 2007) I tried Data->Get External Data->From Web. Pointed it at : http://myhost/myvirtdir/ABCInfoWS.asmx?WSDL&op=testwebservice1
Stuff appears in Excel (albeit with 'The Specified XML source does not refer to a schema' message) but it turns out that it's actually a description of all web-services offered under the same WSDL.
Can anyone tell me how I can get the data from testwebservice via VBA ?
Remove the ?WSDL part from the URL above.
EDIT: What does your webservice return?
Excel's Data -> Import Data expects a tabular structure built with tr & td to get the data in.
If your webservice returns XML, it should work as well.
Sorry it turns out I can't provide enough infromation via comments. This is what is appearing in Excel when I select SOAP 2 and XML:
Just responding to your edits my w-s returns XML and this is what it looks like - it doesn't contain tr/td because it's previously been used as a 'real' webservice .
I've realised that I'm not getting the actual data at all but instead a description of the data that the webservice provides - I guess my URL is still off the beat a bit ?
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetBookJobStatusResponse xmlns="http://www.protecttheguilty.com/">
<GetBookJobStatusResult>
<BookJobStatus>
<JobStatus>string</JobStatus>
<JobType>string</JobType>
<RequestTime>dateTime</RequestTime>
<StartTime>dateTime</StartTime>
<EndTime>dateTime</EndTime>
<PathToOutput>string</PathToOutput>
</BookJobStatus>
<BookJobStatus>
<JobStatus>string</JobStatus>
<JobType>string</JobType>
<RequestTime>dateTime</RequestTime>
<StartTime>dateTime</StartTime>
<EndTime>dateTime</EndTime>
<PathToOutput>string</PathToOutput>
</BookJobStatus>
</GetBookJobStatusResult>
</GetBookJobStatusResponse>
</soap12:Body>
</soap12:Envelope>

Resources