Recently i am stuck with a problem during my development.I have some xml values in a variable (not in a seperate file).I transform the xml using xslt and displayed them using xml control in my webpage.NOw my requirement has been changed and I supposed to display the xml values in Gridview instead of xml control.
string getval= //some stuff where webservice returns xml.
// here I specified content to xmldynamic control
xmldynamic.TransformSource=//my xslt comes here..
Now I want to display the transformed XML in gridview.Is it possible...If possible how..
I surfed through net and identified that if xml is in a file, then xml data source can be used,transformed and define datasource to gridview.But in my case its in a variable.
There a lot of 3rd party Class / Software that does that.
For example for web an this is a powerfull app maybe you dont want this extensive app on your side but consider ExtJS
http://docs.sencha.com/extjs/4.2.2/
///
Also check this old topic for more
Is there an XML Editor with grid view similar to that of XMLSpy?
Try below steps:
1.) Load the XML data in a StringReader
2.) feed this StringReader object to ReadXml() method of DataSet
3.) Now bind this DataSet to your GridView.
This step is always tricky ( May be) as depending on the XML format Many times I needed to modify it slightly as : ds.Tables[0]; or ds.Tables[1]; etc...
Sample:
string xml = #"<?xml version=""1.0"" encoding=""utf-8""?>
<Cameras>
<Camera>
<Model>Canon EOS-1D</Model>
<Price>$5219</Price>
</Camera>
<Camera>
<Model>Canon EOS-1D Mark IV</Model>
<Price>$5000</Price>
</Camera>
</Cameras>";
StringReader sr = new StringReader(xml); // Step 1
DataSet ds = new DataSet();
ds.ReadXml(sr); // Step 2
GridView1.DataSource = ds.Tables[0]; // Step 3
GridView1.DataBind();
Related
I have a table in my database called Employeee.
I need to write the database data to a file.
How do I do it in asp.net mvc C#?
i have tried Datatable approach,but it failed.
Do It the same way you would do in a winform, wpf or so.
For example, if your text document has to be in JSON, use :
using(TextWriter writer = new TextWriter(#"yourfilename.json")){
writer.Write(JsonConvert.SerializeObject(db.Employeee));
}
In my example, don't forget to import the Newtownsoft.Json package.
You can also formate everything in XML, CSV...
I want to update my database over a WCF Data Service and the Entity Framework
from an XML file read from disk.
I am using a DataSet to read the XML file but I don't know how to update the database from the dataset via the Entity Framework part.
I am using a DataSet to read the XML because it works so well but if there
is another way to read XML into the database via Entity Framework that would be great.
The code I want looks like the following.
private void AddToDBFromXMLFile(string xmlFile)
{
// this is reading my XML just fine, but i suspect there is another way to
// get the XML into the
// database, otherwise how can I get the XML into the database via the EF?
DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlFile, XmlReadMode.InferSchema);
// this is working and can update and retrieve my data using the usual methods
var proxy = new TestApp.ServiceReference.TestEntities(new
Uri("http://localhost:4976/TestWcfDataService.svc/"));
// how do I update the database from the dataset?
//proxy << dataset
}
I want to create a component by giving XML source input directly using core service 2011, in SDL Tridion 2011.
I want to write code to create a component by uploading source XML. Using the core service 2011.
Say name of the xml file is helloworld.xml and location is D:\abcd\cdef\all\helloworld.xml.
I have written the code like this, but its not working.
XmlDocument contentxml = new XmlDocument();
contentxml.LoadXml(#"D:\abcd\cdef\all\helloworld.xml");
Response.Write("<BR>" + contentxml.ToString());
component.Content = contentxml.ToString();
ComponentData comp = (ComponentData)client.Create(component, new ReadOptions());
The Response.write is displaying nothing. Correct me if I missed any thing.
It's not creating any component and error is coming.
When i tried this:
XmlDocument contentxml = new XmlDocument();
try
{
contentxml.LoadXml(#"D:\abcd\cdef\all\helloworld.xml");
}
catch (XmlException exp)
{
Console.WriteLine(exp.Message);
}
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
contentxml.WriteTo(xw);
Response.Write("<BR>" + sw.ToString());
component.Content = sw.ToString();
ComponentData comp = (ComponentData)client.Create(component, new ReadOptions());
This time it's showing unable to find UUId: some thing like that.
My helloworld.xml looks like this.
<Content xmlns="uuid:1111eb85-0X11-11f9-1e2X-1X82X78fX920">
<first>Hello World.This is Fisrt field</first>
<second>Hello World.This is second field</second>
</Content>
It would be great if some one share some sample code to do it.
Loading any XML from a a file and trying to create a component won't work unless the XML uses the format the CMS is expecting.
The XML structure of a component in SDL Tridion has some fixed parts (Example Nodes Content, Metadata) plus some flexible parts
(The way you define the fields).
First you need to have the XML with the same structure that the CMS is expecting.
Typically the nodes that should be in your xml are the CONTENT and METADATA, load those in an XML Document and use the Core Service API to
create a component using the content contained in those nodes.
The best way to know how which is the structure of a component based on an schema is to create a sample component using the Tridion UI and
see how the XML is constructed. After that you need to create your XML Sources using that structure.
I posted recently an example of how to create a component using the Core Service, please have a look at that.
Faulted State error while creating component with Core Service
Following this code, you can access the nodes Content and Metadata
componentData.Content = xmlUtil.GetNewXmlNode("Content", schemaData.NamespaceUri);
componentData.Metadata = xmlUtil.GetNewXmlNode("Metadata", schemaData.NamespaceUri);
And replace those with your content
The general outline:
Load the XML from the file into an XDocument / XmlDocument.
Create a new Component by calling GetDefaultData on the client.
Set the Content property of the Component to the XML.
Save the Component by calling Save on the client.
If you haven't already, please have a look at the Core Service API documentation available on SDL Tridion World.
If you have trouble implementing this, please post the code that you have and what you have tried in order to make it work.
Using XmlDocument.LoadXML() expects an XML string as input, as commented by Peter you should use XMLDocument.Load() instead, see here for more details http://msdn.microsoft.com/en-us/library/a8ta6tz4.aspx
When you have passed that hurdle you will need the information Miguel gave in his answer to continue.
<SMSWall run="yes" nonstop="False">
<starttime>10:15:25 PM</starttime>
<endtime>10:15:25 PM</endtime>
</SMSWall>
<MediaPlayer run="yes" nonstop="False">
<starttime>10:15:25 PM</starttime>
<endtime>10:15:25 PM</endtime>
</MediaPlayer>
<Bidding run="yes" nonstop="False">
<starttime>10:15:25 PM</starttime>
<endtime>10:15:25 PM</endtime>
</Bidding>
This is my xml file. Now I want to read the xml, store the value in a variable (and also want the data to read from the string), and leave the .xml file so that it can be deleted.I also want to read the value node wise like:
XmlDocument document = new XmlDocument();
document.Load("Schedule.xml");
XmlNode ht=document.SelectSingleNode("SMSWall/#run");
Your current XML file is not in correct format (i.e save it as .xml and open it with IE, you will give an error). So you should add <root> ... </root> (or other name).
In all I think best way to treat with XML is using System.Xml.Linq library, like XDocument and XElement :
Loading data: XDocument.Load(filename);
Creating item: XElement root = new XElement("Root");
Searching: document.Descendants("Root"), ...
See MSDN for other samples.
I prefer to create a DataSet and then use it like this:
DataSet1 ds = new DataSet1();
ds.ReadXml("test.xml", XmlReadMode.ReadSchema);
//do something
ds.WriteXml("test.xml", XmlReadMode.ReadSchema);
A typed DataSet will provide you typed tables and rows. You can get rows by index or query. You can find more about the here.
I have managed to read in a saved .csv file on my hard disc and get the data to poputlate a data table. I now want to take that data and put it into XML format, then send the XML to a web service. How can I do this?
Thanks for your time.
One option would be to connect to the Web Service using WCF (or ASP.NET Web Services depending on which .NET version you're using). You can then easily fill the proxy classes the framework creates for you and call the service.
The second option would be to use an XmlTextWriter and use that to build the XML document in a MemoryStream. Once you have the XML document built in memory, you can flush the document and send it out to the Web Service.
A third option would be to use LINQ to XML to build the XML document on the fly. Depending on the structure you parse your CSV file into this may be easier/harder for you than using the XmlTextWriter.
XLinq is excellent for putting the data into XML. If you can use an up to date .NET framework I highly recommend it.
There's some info to get started with here (for example)
http://www.c-sharpcorner.com/UploadFile/mahesh/xLinkDoc06202007130827PM/xLinkDoc.aspx
As for sending that XML to a web service, perhaps you should be calling the web service via a client automatically generated using the Visual Studio ServiceReference tool.
You may or may not need to send XML to that -- in many cases services are object-based (i.e. you'd not need XLinq, provided you can parse the CSV into the correct objects.)
/EDIT:
Rough example for calling webservices using WCF:
using(var client = new ServiceReference1.ThirdPartyServiceClient())
{
client.SendSomething("123", "hello");
string output = client.GetSomething();
Console.WriteLine(output);
}
Following code can also be used.
public static List<Student> convertXMLtoList(string filePath)
{
XDocument doc = XDocument.Load(filePath);
List<Student> studentsMarks = doc.Descendants("Student").Select(x => new Student()
{
RollNo = int.Parse(x.Element("roll_no").Value),
Name = x.Element("name").Value,
}).ToList();
return studentsMarks;
}
Where XML looks like
<?xml version="1.0" encoding="utf-8"?>
<Students>
<Student>
<roll_no>1</roll_no>
<name>XYZ</name>
</Student>
...
</Students>
You can find more details at http://bit.ly/1eveLz3. you can find CSV to XML also on this post.
I haven't done this with a datatable, but it work great for other objects.
Try something like this:
public void writeToXML(DataTable inputData, string fileName) {
XmlSerializer xml = new XmlSerializer(typeof(DataTable));
StreamWriter sw = new StreamWriter(fileName);
xml.Serialize(sw, inputData);
sw.Close();
}
Edit: Just noticed you needed to pass it along to a web service. So instead of StreamWriter, use a memory stream...but same idea.
There are different options depending on how robust of a solution you want. Assuming you want to follow the quickest route you should look at the XmlWriter. Using an XmlWriter you can quickly generate an XML document, save to string and then pass along to your web service.