How can we read a xml data file into Windows.Data.Xml.Dom.XMlDocument?
The following code is possible only for System.Xml.XmlDocument.
XmlDocument myxml = XmlDocument.Load("abc.xml");
I read the xml file content to a string, and then use LoadXml():
string fileContent;
StorageFile tileTemplateFile =
await StorageFile.GetFileFromApplicationUriAsync(new Uri(#"<path to file>"));
using (StreamReader reader =
new StreamReader(await tileTemplateFile.OpenStreamForReadAsync()))
{
fileContent = await reader.ReadToEndAsync();
}
XmlDocument tileXml = new XmlDocument();
tileXml.LoadXml(fileContent);
I got most of the code from this answer.
You need to do the following.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(<string representation of your xml>);
Related
I have one function which is
string fileToImport="C:\test.xml";
FileInfo fi = new FileInfo(fileToImport);
XmlDocument doc = new XmlDocument();
doc.Load(fileToImport);
This is point exact location of file.
But I want to use same function and import xml file from website.
For example I have below location.
http://testweb:1000/testdir/test.xml
Then I should open that location xml file and put in to the same way
FileInfo fi = new FileInfo(fileToImport);
XmlDocument doc = new XmlDocument();
doc.Load(fileToImport);
Anyone has best idea to get import this?
Thanks,
You should try
using (var client = new WebClient())
{
using (var reader = new StringReader(client.DownloadString("http://testweb:1000/testdir/test.xml")))
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
}
}
Also you can write this without WebClient&StringReader:
XmlDocument doc = new XmlDocument();
doc.Load("http://testweb:1000/testdir/test.xml");
More at https://msdn.microsoft.com/de-de/library/system.net.webclient(v=vs.110).aspx
I'm converting a JSON string to an XML node like this:
public ActionResult Test(string json)
{
System.Xml.XmlNode myXmlNode = JsonConvert.DeserializeXmlNode("{\"root\":" + json + "}", "root");
How can I save myXmlNode to an external file, say test.xml?
Thanks
This should do it:
var xdoc = XDocument.Load(new StringReader(myXmlNode.ToString()), LoadOptions.None);
xdoc.Save(#"c:\temp\test.xml", SaveOptions.None);
UPDATE:
using (StreamWriter writer = new StreamWriter(Server.MapPath("~/test.xml")))
{
writer.WriteLine(myXmlNode.OuterXml);
}
XmlDocument doc = new XmlDocument();
doc.LoadXml(myXmlNode);
XmlTextWriter writer = new XmlTextWriter("yourfilename.xml",null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
I want to extract a xlsx file to get the sheet1.xml file. Now I am struggling with the package and PackagePart. I think the most obvious way is to read that particular file and copy the content to the XmlDocument
This is what i have this far:
XmlDocument doc = new XmlDocument();
using (Package package = ZipPackage.Open(xlsFile, FileMode.Open, FileAccess.Read))
{
foreach (PackagePart part in package.GetParts())
{
var target = Path.GetFullPath(Path.Combine(tempFolderPath, part.Uri.OriginalString.TrimStart('/')));
var targetDir = target.Remove(target.LastIndexOf('\\'));
if (!Directory.Exists(targetDir))
Directory.CreateDirectory(targetDir);
using (Stream source = part.GetStream(FileMode.Open, FileAccess.Read))
{
FileStream targetFile = File.OpenWrite(target);
byte[] bytes = new byte[source.Length];
source.Read(bytes, 0, (int)source.Length);
source.Close();
//source.CopyTo(targetFile);
//doc.Load(source.Write());
//targetFile.Close();
}
}
}
I am using .net 3,5 so I cannot use the Stream source.CopyTo methods.
I would like to copy the contents of the Sheet1.xml to the doc of the XmlDocument class..
Thanks!
Paul
You can use XmlDocument.Load overload which takes a Stream:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(part.GetStream(FileMode.Open, FileAccess.Read));
Once loaded, you then can use XmlDocument.Save:
xmlDoc.Save(target);
I've already written code to parse my xml file with an XmlReader so I don't want to rewrite it. I've now added encryption to the program. I have encrypt() and decrypt() functions which take an xml document and the encryption algorithm. I have a function that uses an xml reader to parse the file but now with the xml document I'm not sure how to create the xmlreader.
The question is how to save my xml document to a stream. I'm sure it's simple but I don't know anything about streams.
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load(filep);
Decrypt(doc, key);
Stream tempStream = null;
doc.Save(tempStream); // <--- the problem is here I think
using (XmlReader reader = XmlReader.Create(tempStream))
{
while (reader.Read())
{ parsing code....... } }
You can try with MemoryStream class
XmlDocument xmlDoc = new XmlDocument( );
MemoryStream xmlStream = new MemoryStream( );
xmlDoc.Save( xmlStream );
xmlStream.Flush();//Adjust this if you want read your data
xmlStream.Position = 0;
//Define here your reading
Writing to a file:
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<FTPSessionOptionInfo><HostName>ftp.badboymedia.ca</HostName></FTPSessionOptionInfo>");
using (StreamWriter fs = new StreamWriter("test.xml"))
{
fs.Write(doc.InnerXml);
}
}
I realize this is an old question, but thought it worth adding a method from this nice little blog post. This edges out some less performant methods.
private static XDocument DocumentToXDocumentReader(XmlDocument doc)
{
return XDocument.Load(new XmlNodeReader(doc));
}
try this
XmlDocument document= new XmlDocument( );
string pathTmp = "d:\somepath";
using( FileStream fs = new FileStream( pathTmp, FileMode.CreateNew ))
{
document.Save(pathTmp);
fs.Flush();
}
i have xml what i get as byte array, whats the best way to get the xml string out of it? I was tryng to use xmltextreader and memorystream but with no success..
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(buffer);
doc.LoadXml(xml);
OR
XmlDocument doc = new XmlDocument();
MemoryStream ms = new MemoryStream(buffer);
doc.Load(ms);
This assumes your data has UTF8 encoding which is the usual for XML. Also buffer here is the byte array.
Assuming your xml is in the default 'UTF8' encoding., you could do something like this;
string xml = System.Text.UTF8Encoding.UTF8.GetString(bytes);
System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument().LoadXml(xml);
Or this;
XmlDocument doc = new XmlDocument();
using (MemoryStream ms = new MemoryStream(buffer))
{
doc.Load(ms);
}
Based on the Encoding, you can do
string xmlString = System.Text.UTF8Encoding.UTF8.GetString(bytes);
and use the string
XmlTextReader reader = new XmlTextReader(new StringReader(xmlString));
Take a look at the System.Text.Encoding.UTF8 class. It should let you convert youre byte array into a UTF8 string.