Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a stylized XML file to a text file?

Status
Not open for further replies.

collegian

Programmer
Jul 1, 2010
64
US
Hello All,

I am using the <asp:xml> tag on my front end and I am supplying the Document Content(data source) as well as the transform source(XSL file) on the back end.Now, I want to write the transformed result to a text file. How can I do it? Any suggestions will be greatly appreciated.

Thanks.
 
Thanks for the reply.I know I can use the TextWriter and StreamWriter to write out the values.However, my issue is how to obtain those values? How to fetch the transformed XML without having to parse the source HTML?
 
you have the xml document and xslt document, transform the xml and push to the stream writer. example:
Code:
var reader = new XmlReader(xml file);
var transformer = new XsltDocument(xslt file);

var formattedOutput = transformer.Transform(reader);

var writer = new StreamWriter();
writer.Write(formattedOutput);
since you are operating in asp.net there will probably be security restrictions on where you can save the file. this won't show up locally because the server and client are the same computer/user with the same rights. Once deployed (testing or production) this may become an issue.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I am getting FileNotFound Exception:Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\pub2endnote.xsl'.I have my XSL file in the same directory as my .aspx.cs file. I am not sure why it is looking up this directory?

Also,the text file obtained is having html tags within it.Is there any way to get rid of those?

Here is my code fragment:

DataSet ds = new DataSet("pubs");

OleDbDataAdapter sda_type = new OleDbDataAdapter(pubtype_sql, conn);
sda_type.Fill(ds, "pubtype");

sda_year.Fill(ds, "pubyear");

//Response.Flush();

int currYear = DateTime.Now.Year;
string defaultWhere = " and year(pubdate) >= " + (currYear - 1 + " or inpress = 1");
OleDbDataAdapter sda = new OleDbDataAdapter(pub_sql + defaultWhere + pub_sql_order, conn);
sda.Fill(ds, "publications");
xmlPubList.DocumentContent = ds.GetXml();
xmlPubList.TransformSource = pub_xsl_web;

XmlDocument doc = new XmlDocument();
doc.LoadXml(ds.GetXml());
doc.Save("CurrentXml.xml");
XslTransform myXslTransform = default(XslTransform);
XPathDocument myXPathDocument = default(XPathDocument);


myXslTransform = new XslTransform();
myXslTransform.Load("pub2endnote.xsl");
System.IO.StringWriter stWrite = new System.IO.StringWriter();

myXslTransform.Transform("CurrentXml.xml", "C:\\Documents and Settings\\collegian\\Desktop\\EndNoteFormat.txt");
 
2 big red flags:
C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\
C:\\Documents and Settings\\collegian\\Desktop\\

this won't work when you deploy. it might work now because the server and client are the same machine. not to mention this should be a configurable option, not hard coded.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Yes,I know that.I just wanted to test if my code is working or not.

How do I get around this?Should I create a key in web.config and then access it? I tried doing that for the .xsl file but it doesn't work.It just looks at location C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0.
 
inject the value into the page. this can be done any number of ways. it can be as simple as an AppSettings key value pair, a custom configuration section, or a full blown inversion of control framework (IOC).

it's probably looking in the C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0 directory because you didn't assign the full path.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I tried to specify the xsl file in the web.config using a key,value pair but it doesn't seem to work. I specified the following key-value pair under the <appSettings> tag:

<add key="PUB2ENDNOTE_XSL" value="pub2endnote.xsl"/>

Both web.config and the .xsl files are in the same directory.

In the code behind,I used the following code:

string pub_xsl_web=ConfigurationSettings.AppSettings["PUB2ENDNOTE_XSL"];

and then I get the exception at this line in the above code in the code behind,stating that the file could not be found at the location specified:
myXslTransform.Load(pub_xsl_web);


I am not sure why it doesn't work.Also,how do I use a custom configuration section, or a full blown inversion of control framework (IOC)?
 
you need the absolute path to the file, not a relative path. note that in the web environment a relative path is [tt]~\path\to\file.ext[/tt]

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I am not sure if I understand you. If I hard code the absolute path then the website won't work when I publish it to some other location.
I thought ~ symbol represented the root directory of the project so anything inside it will be represented by ~/... but this doesn't work in the current scenario.
 
hard code the configuration file. that's the point of the a config file. you can change the values, without compiling the system. if you use a relative path, you then need to map it.
Code:
var absolutePath = Server.MapPath("~\path\to\file.ext");

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
This means that whenever I publish the website I need to go to web.config and change the path each time?
 
it depends on how you setup your website and how you push updates. typically there are a handful of web.config changes that need to be made with the initial deployment. that's the point of the config file. otherwise the value can be hard coded.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top