How i can open an xml or xsl file inside the same assembly from source code in C#?
Any ideas?
Google found this
using System;
using System.IO;
using System.Reflection;
using System.Xml;
class Application
{
static void Main(string[] args)
{
Stream s =
Assembly.GetExecutingAssembly().GetManifestResourceStream("File1.xml");
XmlDocument xdoc = new XmlDocument();
using (StreamReader reader = new StreamReader(s))
xdoc.LoadXml(reader.ReadToEnd());
}
}
Related
I have the following XML in an API
I wish to read the data from the tags and build a CSV file with the values.
My code so far,
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string url = "https://localhost:5001/api/Scheduler/GetScheduler";
WebRequest request = WebRequest.Create(url);
try
{
WebResponse response = request.GetResponse();
using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
{
XDocument xmlDoc = new XDocument();
try
{
xmlDoc = XDocument.Parse(sr.ReadToEnd());
Console.WriteLine(xmlDoc.Root.Element("ORD_NAME").Value);
}
catch (Exception)
{
// handle if necessary
}
}
}
catch (WebException)
{
// handle if necessary
}
}
}
}
I can see the data being read in, but xmlDoc, but xmlDoc.Root.Element("ORD_NAME").Value is NULL.
How do I get the data from the stream?
Thanks.
I think you want e.g
using (var stream = response.GetResponseStream())
{
XDocument xmlDoc = XDocument.Load(stream);
Console.WriteLine(string.Join("\n", from row in xmlDoc.Root.Elements("Scheduler") select string.Join(";", row.Elements().Select(e => e.Value))));
}
.NET probably has some better APIs to construct CSVs than using LINQ to XML directly, the above will fail to quote value or escape separators.
Say I have an XSLT file like below:
`<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-
prefixes="xs math"
version="3.0">`
....... and so on.
I need the output as 3.0 because the above file has version="3.0". I want to use C# to get this given the XSLT is in string format
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
string version = (string)doc.Root.Attribute("version");
}
}
}
Use XElement.Parse(yourString).Attribute("version").Value, where you add using System.Xml.Linq;. See https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview for details of the used API.
is it possible to rewrite XML file according to other schema XSD using c# ?
this is XML file
this is the current schema XSD file
and this is the new schema same everything but changed node names
so how to get a new XML from the old one based on the new XSD using c#?
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication13
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> shipTo = doc.Descendants("shipto").ToList();
foreach (XElement ship in shipTo)
{
ship.Element("name").ReplaceWith(new XElement("FullName", (string)ship.Element("name")));
ship.Element("address").ReplaceWith(new XElement("FirstAddress", (string)ship.Element("address")));
ship.Element("city").ReplaceWith(new XElement("homeTown", (string)ship.Element("city")));
ship.Element("country").ReplaceWith(new XElement("HomeLand", (string)ship.Element("country")));
}
}
}
}
I want to read XML document from a property which is created in edit mode of Episerver.
I have made one property of type 'URL to Document'.
When I try to fetch it from code behind, it gives only file path. I am not able to read the content of XML file which is uploaded in property.
string XMLContent = Currentpage.Getproperty<string>("XMLFile");
Can anyone help out on this?
You need to load the file as well. Something like this:
var path = CurrentPage["XMLFile"] as string;
if (HostingEnvironment.VirtualPathProvider.FileExists(path))
{
var file = HostingEnvironment.VirtualPathProvider.GetFile(path) as UnifiedFile;
if (file != null)
{
using (var stream = file.Open())
{
// Here is your XML document
var xml = XDocument.Load(stream);
}
}
}
You can also load the file content by using the local path on disk, file.LocalPath.
try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string XMLContent = "";
//using XML
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(XMLContent);
//using xml linq
XDocument doc2 = XDocument.Parse(XMLContent);
}
}
}
I want to read in one file and write to certain columns in the other
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
using (StreamWriter sw = new StreamWriter("AEJoinerDataTemplate.csv"))
// using streamwriter to write to a text file
using (StreamReader sr = new StreamReader("AutoEnrolment.csv"))
// using streamreader to read a text file
{
while ((txtline = sr.ReadLine()) != null)
{
A very flexible way would be to first put your records into objects and then write those fields of the objects you would like to output to a new file.
You could for example use the library below to read objects from a file and write objects to a file.
FileHelpers