How to save XML document in web method in server path in asp.net?
XmlDataDocument doc = new XmlDataDocument(ds);
XmlElement element = doc.DocumentElement;
return ds.GetXml();
string _XMLFileName = HttpContext.Current.Server.MapPath(#"Data\error1.xml");
ds.WriteXml(_XMLFileName);
This should work:
doc.Save(_XMLFileName)
Looking at your comments, can you try using HostingEnvironment.MapPath. Read more on MSDN.
In your case it would be
string _XMLFileName = HostingEnvironment.MapPath("~/App_Data/Data/error1.xml");
HostingEnvironment maps path relative to your hosting location, so you will need to provide ~ for root location. In the example, the file will be inside your root directory of application, under App_Data/Data folder.
Hope this works for you.
This is how I am doing it at the moment..
Dim path As String
path = Server.MapPath("nameOfXMLDoc.xml")
Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
doc.Load(path)
And it works for me.. found the answer here: http://www.codeproject.com/Articles/165438/Getting-XML-Data-from-Webservice-to-Infopath
Related
I have a function which works in the development environment but I need to change the path so it resolves properly at the host servers.
This code line;
doc.Load("H:\Website_Sep2012\OtherDataFiles\dataXML.xml") 'this needs to be changed to the server path
within this function
Public Shared Function GetList(ByVal nodestring As String) As List(Of String)
Dim doc As New XmlDocument()
'Load XML from the file into XmlDocument object
doc.Load("H:\Website_Sep2012\OtherDataFiles\dataXML.xml") 'this needs to be changed to the server path
Dim root As XmlNode = doc.DocumentElement
'Select all nodes with the tag paramter indicated by the nodestring variable
Dim nodeList As XmlNodeList = root.SelectNodes(nodestring)
Return (From node As XmlNode In nodeList Select node.InnerText).ToList()
End Function
Replacing the string with Server.MapPath("~/OtherDataFiles\dataXML.xml") does not work as Server is not available in that scope. Any ideas how to resolve this path
You could also try:
System.Web.Hosting.HostingEnvironment.MapPath() No need for HttpContext
OR
using System.Web;
HttpContext.Current.Server.MapPath("~/OtherDataFiles/dataXML.xml");
The Server can always be reached like this:
string filePath = System.Web.HttpContext.Current.Server.MapPath("~/OtherDataFiles/dataXML.xml");
doc.Load(filePath);
If inside a class library project you can just add reference to System.Web assembly.
I am trying to use the value of <Directory> in my following piece of code:
Public Function GetFile() As String
Dim di As New DirectoryInfo(< Directory >)
Dim files As FileSystemInfo() = di.GetFileSystemInfos()
Dim newestFile = files.OrderByDescending(Function(f) f.CreationTime).First
Return newestFile.FullName
End Function
Is there any way i can call the value stored in the xml file in my code?
Andy's answer is good, but in VB it's even easier.
Dim xmlDoc As XDocument
Dim dir as String
xmlDoc = XDocument.Load("XMLFile1.xml")
dir = xmlDoc.<ServerList>.<Server>.<Directory>.First().Value;
Or even easier if the XML file will never have more than one <Directory> element that you care about:
dir = xmlDoc...<Directory>.First().Value;
To answer your comment on Andy's answer:
dir = (From server as XElement in xmlDoc...<Server>
Where server.<ServerName>.First().Value = requiredServer
Select server.<Directory>.First().Value)(0);
As you are clearly familiar with Linq, you can operate on the Xml using System.Xml.Linq.
Apologies, this is in c#.
var xDoc = XDocument.Load("XMLFile1.xml");
var dir = xDoc.Element("ServerList").Elements("Server").Elements("Directory").First().Value;
If you have the Xml stored in a string replace XDocument.Load with XDocument.Parse.
Obviously you'll have to defend against parse errors, file missing and schema inconsistencies in your production code.
You can use this http://support.microsoft.com/kb/301225
Hi i have the following code to read the xml file and change the value of particular node ,but i want to do the same using xmltextreader or xmlreader, i am trying to avoid the statement doc.Save(System.Web.HttpContext.Current.Server.MapPath("Data/Example.xml")); , which has a direct reference to my physical file.
XmlDocument doc = new XmlDocument();
string xmlFile = System.Web.HttpContext.Current.Server.MapPath("Data/Example.xml");
doc.Load(xmlFile);
XmlNodeList xmlnode = doc.GetElementsByTagName("value");
xmlnode[0].ChildNodes[0].Value = 23;
doc.Save(System.Web.HttpContext.Current.Server.MapPath("Data/Example.xml"));
Well something's going to have to have a reference to the file. However, you could easily change your code to simply accept a Stream (which would have to be readable, writable and seekable):
private static void ChangeDocument(Stream stream)
{
XmlDocument doc = new XmlDocument();
doc.Load(stream);
XmlNodeList xmlnode = doc.GetElementsByTagName("value");
xmlnode[0].ChildNodes[0].Value = 23;
stream.Position = 0;
doc.Save(stream);
stream.SetLength(stream.Position); // Truncate the file if it was longer
}
It's somewhat ugly, admittedly...
Of course you could always pass in the filename itself - your MapPath call would still be in a higher level method, which may be all you're trying to achieve:
private static void ChangeDocument(string filename)
{
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNodeList xmlnode = doc.GetElementsByTagName("value");
xmlnode[0].ChildNodes[0].Value = 23;
doc.Save(filename);
}
One final aside - if you're using .NET 3.5 or higher, I'd strongly recommend using LINQ to XML as a rather nicer XML API.
You can not write or update XML data using XmlTextReader or XmlReader ansectors, use XmlTextWriter or XmlWriter ancestors, ot XmlSerializer.Serialize(..) method as alternative.
You can not avoid the statement and alike,
doc.Save(System.Web.HttpContext.Current.Server.MapPath("Data/Example.xml"));
which has a direct reference to your physical file, because you have to write the changed nodes back to the origin file again, otherwize you have to switch your system from using file-based approach (ASP.NET web site using file-based data storage) to database-driven approach (ASP.NET web site using the database server).
As alternative, use your own XML data in your SQL server, then load and update it using EF of ORM.
i am using asp.net 2.0 in my project using file upload control , so browse the drive and select the file like path(D:\user doc new 2011\Montana\MT_SUTA_2010-2011.html)
but in my code seeing error is could not find the file path is
(D:\Paymycheck\OnlineTaxUserDocumentation-1\TaxesDocument\MT_SUTA_2010-2011.html) actually it is application path and take the filename only my code is
if (FileUpload.HasFile)
{
string filepath = Server.MapPath(FileUpload.FileName);
string strHTML = File.ReadAllText(filepath);
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] file1 = encoding.GetBytes(strHTML);
int len = file1.Length;
byte[] file = new byte[len];
docs.TaxAuthorityName = ddlTaxAuthority.SelectedItem.Text;
docs.TaxTypeID = ddlTaxType.SelectedValue;
docs.TaxTypeDesc = ddlTaxType.SelectedItem.Text;
docs.EffectiveDate = Convert.ToDateTime(txtEffectiveDate.Text);
docs.FileName = f1;
if (ddlTaxAuthority.SelectedValue == "FD")
{
docs.Add(strHTML, file1);
}
}
error occoured in this line
string strHTML = File.ReadAllText(filepath);
i can try like this also
string FolderToSearch = System.IO.Directory.GetParent(FileUpload.PostedFile.FileName).ToString();
string f = Path.GetDirectoryName(FileUpload.PostedFile.FileName);
string f1 = FileUpload.FileName;
string filepath = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
string strFilePath = FileUpload.PostedFile.FileName;
string file1234 = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
string filepath = FileUpload.PostedFile.FileName;
so how to get the total path drive to file
pls help me any one
thank u
hemanth
Because your are using Server.MapPath, which according to MSDN "maps the specified relative or virtual path to the corresponding physical directory on the server." You have to first call FileUpload.SaveAs method to save file on server then try to read it's content.
If you want the total client side drive path like D:\user doc new 2011\Montana\MT_SUTA_2010-2011.html for the file MT_SUTA_2010-2011.html to be uploaded via fileupload control, then try using System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName).
This will surely return the client side path of the file.
Try this it will work
string filepath = System.IO.Path.GetFullPath(fuldExcel.PostedFile.FileName);
fuldExcel.SaveAs(filepath); //fuldExcel---is my fileupload control ID
What object can I use to get the current PageName.aspx (including the extension .aspx) from the URL? I can't find what object and method to allow me to grab this when I'm on a page.
Note that sometimes, on shared hosting like GoDaddy, you might not have the permission to create a new FileInfo object. Yes, believe it.
So I suggest you use this snippet:
string fullPath = /* System.Web.HttpContext.Current. (optional in most cases) */ Request.Url.AbsolutePath;
string fileName = System.IO.Path.GetFileName(fullPath);
Enjoy :-)
Pino here's the source lil man: http://www.devx.com/tips/Tip/42433
string sPagePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oFileInfo = new System.IO.FileInfo(sPagePath);
string sPageName = oFileInfo.Name;
http://www.aspcode.net/Get-current-page-name.aspx
public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}
Request.Url.AbsolutePath
Split about '/', last item is your file name.
Path.GetFileName(Request.PhysicalPath) can be used to fetch the actual file name
I used: Request.Url.LocalPath, which gave me "/default.aspx", even when full URL would be https://www.example.com/?foo=bar. You just need to strip the leading /.
How about this:
var pageName = System.IO.Path.GetFileName(Request.Url.ToString());
Response.Write(pageName);
string pageName = Path.GetFileName(Request.Path);