Generate XML Schema From XML String - c#

I have an XML String that I want to load into an Excel worksheet. I've almost got it to work but I need to be able to generate the schema for passing to XmlMaps.Add:
String xml; //This is already populated with an XML String; I want to get the schema based on the xml structure
Workbook w = Globals.ThisAddIn.Application.ActiveWorkbook;
w.XmlMaps.Add(***Need schema here***, "root node");
Is it possible to generate the schema programmatically here? I am receiving this XML string from a web service.

Related

load xml file after reading in string

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.

Get IPublishedContent To XDocument

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
}

how to pass xml data to gridview?

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();

How to read, store and delete xml data in C#

<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.

Referencing external xml file through DTD Entity

Is there a way of returning an xml file from a REST webservice. I want to be able to call the server, dynamically create the file and return as if I'd simply returned the xml file as stored on disk.
I've managed to get fairly close by returning an XMLElement:
public XmlElement Airports()
{
//Dynamically build up and return
}
But when I referenced the location from a DTD it doesn't seem to behave in the same way e.g.
This works:
ENTITY XmlFile SYSTEM "http://localhost:59736/MyXmlFile.xml"
But this doesn't
ENTITY XmlFile SYSTEM "http://localhost:59736/MyService.svc/MyMethod"
There must be some subtle difference in the headers or something...
I assume your are not returning a file name or anything like that but the file contents as an XML response. In that case it really is no different if you return the contents of an existing file or generate the contents on the fly.

Categories