I am new to the umbraco world. I am uploading an xml file using umbraco. I have a method that parses xml document on the backend using
System.Xml.Linq.
I am trying to get that file into an XDocument.
On the front end I have the following call that returns data
IPublishedContent doc = Umbraco.Media(CurrentPage.XMLfile);
but I dont know how to get that into a format that I can parse.
You can use TypedMedia method to have strongly typed object and use it's properties to retrieve / load file (e.g. from URL where it's stored inside the filesystem on your server).
Check: https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/media-picker and then use your preferred way to load up XML document from specific location.
It will look like e.g (untested, but should point you in the right direction):
var typedDocument = Umbraco.Media(CurrentPage.XMLfile);
if(typedDocument != null) {
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(typedDocument.Url));
// ... do whatever you want with your doc
}
Related
How to save a below Xml web page content as a XML document in local drive using C# .Net.
URL is as below :
http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote
I want to load few field values into oracle DB table in a daily basis. Please help me to code.
As far as saving the XML as a local file, I have put together the following code.
System.Net.WebClient wc = new System.Net.WebClient();
string webData = wc.DownloadString("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote");
System.IO.File.WriteAllText(#"C:\YOUR_FILE_PATH\FILE_NAME.xml", webData);
Just replace YOUR_FILE_PATH and FILE_NAME with where you want to save the data and your desired file name and it should be good.
take a look at XMLDocument class
you can use method Load to load your xml and Save to save it to a file
https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx
First you need to generate the C# classes for your XML.
https://msdn.microsoft.com/en-us/library/hh371548(v=vs.110).aspx
The summary is you copy the xml text and then in a code file but outside of a class, click menu Edit -> Paste Special -> Paste XML as Classes
https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx
Then you can then load the xml file using XmlSerialzer
From there you can use push it to your oracle DB through Entity Framework Connection.
code to deserialize into a list object:
string url = "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(list));
HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse();
list mylist = (list)xmlSerializer.Deserialize(aResponse.GetResponseStream());
Then you can use mylist as any object.
I have read an xml file as a string due to cryptography.
string xmlString = System.IO.File.ReadAllText("../../liberal.xml");
//how to load xml document here?
XmlDocument xmlDo = new XmlDocument();
xmlDo.Load("../../liberal.xml");
The above code throws error and doesn't load.
XML file doesn't have any root elements and right now liberal XML file looks like this dasjkhf8dfkbhdflak3kjbdf+fafas(safasasdfjgdskalfguv.ng;FHSDAFKLASDF.
Couldn't load xml document with this data format. Only if I can load XML document I will be able to use their properties to add new values to the xml file.
Update1:
I decrypted the xml and placed in a string, but couldn't load the xml document with that string.
string retValue;
XmlDocument dec = new XmlDocument();
dec.Load(retValue);
retValue string has values like this.
<Product><Type>Metal</Type><Department>Foundry</Department></Product>
Error Message
Illegal characters in path.
Really appreciate any help with this.
You're using the XmlDocument.Load(string) method which accepts a path to an XML file. You need to use the XmlDocument.LoadXml(string) method which accepts any valid XML markup. Two completely different parameters. Example:
// XmlDocument.LoadXml(string)
string decryptedMarkup = "<Product><Type>Metal</Type>"
+ "<Department>Foundry</Department></Product>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(decryptedMarkup);
// XmlDocument.Load(string)
string pathToFile = "test.xml";
XmlDocument xmlDoc2 = new XmlDocument();
xmlDoc2.Load(pathToFile);
For further information, take a look at:
XmlDocument.Load(string)
String parameter:URL for the file containing the XML document to load. The URL can be either a local file or an HTTP URL (a Web address).
XmlDocument.LoadXml(string)String parameter:String containing the XML document to load.
I'm kind of new to XML files in C# ASP.NET. I have a XML in the below format:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Installation>
<ServerIP>192.168.20.110</ServerIP>
<DB_Name>USTCKT1</DB_Name>
<Username>jorame</Username>
<Password>Cru$%e20</Password>
<Table_PreFix>TCK</Table_PreFix>
</Installation>
I need to change the values within each element. For example, when an user clicks I should be able to replace 192.168.20.110 with 192.168.1.12.
How can I accomplish this? Any help will be really appreciated.
You should look at using the methods in the XDocument class. http://msdn.microsoft.com/en-us/library/bb301598.aspx
Specifically look at the methods: Load(string) - to load an XML file, Element() - to access a specific element and Save(string) - to save the XML document. The page on Element() has some sample code which can help.
http://msdn.microsoft.com/en-us/library/system.xml.linq.xcontainer.element.aspx
You can do something like this using the XDocument class:
XDocument doc = XDocument.Load(file.xml);
doc.Element("Installation").Element("ServerIP").Value = "192.168.1.12";
//Update the rest of the elements
doc.Save(file.xml);
More Details
If you run into namespace issues when selecting your elements you will need to include the xml namespace in the XElement selectors eg doc.Element(namspace + "Installation")
In general, you can do it in the following steps:
Create a new XmlDocument object and load the content. The content might be a file or string.
Find the element that you want to modify. If the structure of your xml file is too complex, you can use xpath you find what you want.
Apply your modification to that element.
Update your xml file.
Here is a simple demo:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml"); // use LoadXml(string xml) to load xml string
string path = "/Installation/ServerIP";
XmlNode node = xmlDoc.SelectSingleNode(path); // use xpath to find a node
node.InnerText = "192.168.1.12"; // update node, replace the inner text
xmlDoc.Save("file.xml"); // save updated content
Hope it's helpful.
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.