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