473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Overwriting the files

hello all,
I have done C# web service for extracting xml file nodes and generate the files for the correcsponding nodes in that parsed xml. When second time i want to overwrite those files I dont understand how to do it. Could anyone please help me out how to do and here is my whole code.

the parameters for this methods are sourceXml(the xml that has to be parsed, xslt file for this xml file, targetPath to store result parsed xml file, fileXsltDoc for the xslt for the node xml)

[WebMethod]
public string uploadDocument(string sourceDoc,string xsltDoc,string targetPath, string fileXsltDoc)
{
String resultDoc;
int posExt1 = sourceDoc.IndexOf(".xml");
sourcePath = sourceDoc.Substring(0,posExt1);
Console.WriteLine(sourcePath);
int posExt2 = xsltDoc.IndexOf(".xsl");
xslt = xsltDoc.Substring(0,posExt2);
Console.WriteLine(xslt);
try
{
//XPathDocument myXPathDocument = new XPathDocument(sourceDoc);
XmlDocument myXPathDocument = new XmlDocument();
myXPathDocument.Load(sourceDoc);

XslTransform myXslTransform = new XslTransform();

//Generating directory and file
string trgtTxtPath = Path.GetFullPath(targetPath);
DirectoryInfo di = new DirectoryInfo(trgtTxtPath);

// Create the directory only if it does not already exist.
if (di.Exists == false)
di.Create();

string fNamePath= Path.GetFileName(sourcePath);
Console.WriteLine("File Name: " +fNamePath);

// Create a subdirectory in the directory just created.
DirectoryInfo dis = di.CreateSubdirectory(fNamePath);
if (dis.Exists == true)
{
Console.WriteLine("Subdir already exists!");
}
else
{
dis.Create();
Console.WriteLine("SubDir's Created Successfully");
}

fullPath = Path.GetFullPath(trgtTxtPath);
Console.WriteLine(fullPath);

//moving the base xml file into the subdirectory
subdir = fullPath+ "/"+fNamePath;
string fPath = fNamePath + ".xml";
string fFullPath = subdir + "\\"+ fPath;

FileInfo file = new FileInfo(sourceDoc);
file.CopyTo(fFullPath,true);

//Generating directory and file
string rsltFileName = fNamePath + (".xml"+".xml");
resultDoc = subdir + "\\" + rsltFileName;
FileStream resultFile = File.Create(resultDoc);

XmlTextWriter writer = new XmlTextWriter(resultDoc, null);
myXslTransform.Load(xsltDoc);

//Transform Basic Xml and Xslt for the Basic Xml meta data
myXslTransform.Transform(myXPathDocument, null, writer,null);
File.SetAttributes(resultDoc,FileAttributes.Hidden );

# region Code for the DocLib_Erhebungs .xml's generation

doclibFilesCreate(sourceDoc,subdir, fileXsltDoc);

#endregion Code for the DocLib_Erhebungs .xml's generation
writer.Flush();
writer.Close();
return "OK";

}
catch (FileNotFoundException filexc)
{
Console.WriteLine ("Exception: {0}", filexc.ToString());
return "Error:"+filexc;
}
catch (Exception exc)
{
Console.WriteLine ("Exception: {0}", exc.ToString());
return "Error:"+exc;
}
// return "OK";
}

public void doclibFilesCreate(string sourceDoc,string targetSubPath, string fileXsltDoc)
{
string docFileRslt;
XmlDocument myXPathDocument = new XmlDocument();
myXPathDocument.Load(sourceDoc);
int posExt3 = fileXsltDoc.IndexOf(".xsl");
fileXslt = fileXsltDoc.Substring(0,posExt3);
Console.WriteLine(fileXslt);

//Generating directory and file
//string subTrgtTxtPath = Path.GetFullPath(fullPath);

XmlNodeList elements = myXPathDocument.SelectNodes("//Item/DocLib_Erhebungsmittel");
foreach (XmlElement element in elements)
{
fileName = element.GetAttribute("ows_FileLeafRef");

int len1 = fileName.Length;
int lastHash = fileName.IndexOf('#');
int len = len1 - lastHash ;
string fName = fileName.Substring(lastHash+1,len-1);
Console.WriteLine(fName);

//Generating directory and file
string docFileName = fName +".xml";
docFileRslt = targetSubPath + "\\" + docFileName;


//Create the XsltArgumentList.
XsltArgumentList xslArg = new XsltArgumentList();

//Create a parameter of a string
xslArg.AddParam("Target_FileLeafRef", "", fileName);


XslTransform fileTransform = new XslTransform();
fileTransform.Load(fileXsltDoc);
// File.Create(docFileRslt);
XmlTextWriter fileWriter = new XmlTextWriter(docFileRslt, null);
//Transform Basic Xml and Xslt for the Basic Xml meta data
fileTransform.Transform(myXPathDocument, xslArg, fileWriter);
File.SetAttributes(docFileRslt,FileAttributes.Hidd en);
fileWriter.Close();
GetVirtualDirectories(subdir,fName);

}
}


I would appreciate if anyone can help.

Bye
Aug 1 '07 #1
0 1053

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
19
by: tweak | last post by:
I have been messing around with buffers, and I found it peculiar that the code below will run without a segmentation fault. As far as I know, overwriting the allocated space from a call to...
11
by: Steven Jones | last post by:
I have a C program that prints out two lines as follows: Line 1 Line 2 What I would like is for this program to sleep for one second, and then print out two more lines, overwriting the...
1
by: Kyote | last post by:
I'm wanting to copy/move files from 1 directory to another in my program. I'm even doing a bit of renaming to help these specific files conform slightly to my preferred naming conventions to help...
4
by: vancehome | last post by:
I have a series of macros running everynight creating tons of excel files (using transfer spreadsheet). I then have another program that zips them up and emails them all over the place. It then...
6
by: Frank Rizzo | last post by:
It seems that I can overwrite a .net assembly even though it is in use (in most cases). In some cases, the OS says File in use. Is there a write-up somewhere on when a .net assembly can be messed...
3
by: PokerMan | last post by:
HI When i publish my site, it overwrites all files. but when i have some large files included such as mp3s. It overwrites these and even tho i havent touched them it reuploads all of them....
0
by: nettynet | last post by:
I'm really new in java. I'm trying to read 8000 URL files and write to a file. It's a kind of combining all files together...not overwritten. This is the URL of my all files...
27
by: Jason | last post by:
Hi, I need to open an existing file, seek to a position at X number of bytes, and write out Y number of bytes overwriting any existing bytes, but no erasing any other data. Is this possible? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.