locked
Delete a line in XML file using C# RRS feed

  • Question

  • I'm working on a project to load an XML file and create Release Notes for our software.  Our company uses Test Track Pro for defect database.  I'm exporting the XML file from TTP.  Within  the XML document is the following line. 

    <!DOCTYPE TestTrackData SYSTEM "TestTrackData.dtd">


    This is the second line in the XML file.  If I delete this file manually, my program works fine.  I don't want to have to ask the project managers to go into the XML file to delete this line, so I'm hoping to find a way programmatically. 

    The Exception error I get with this line just states that it can not find this dtd file.


                        //Load XML into reader
                        XmlTextReader reader = new XmlTextReader(openFileDialog1.FileName);
                      
                        //Instantiate an XPathDocument using the reader
                        XPathDocument xpathdoc = new XPathDocument(reader);  <--- This is the line that gives the exception


    If I write in to prohibit DTD, I get an exception on the same line just stating that it is prohibited. 

    I have tried the (try/catch), but I'm not really good at those.


    I can't simply wright


    try
    {XPathDocument xpathdoc = new XPathDocument(reader);}
    catch{}

    Because I'm instantiate in the try. 

    Is there a way to delete this line or over look it?

    So, any suggestions?

    Thanks!


    Nicole
    • Moved by jack 321 Friday, June 20, 2008 9:38 AM off topic
    Wednesday, June 18, 2008 3:04 PM

Answers

All replies

  • Hello,

    The try/catch should like the following.

    // Object Reference  
                object obj = null;  
                try 
                {  
                    // Instantiation  
                    obj = new object();  
                    // Do something.  
                }  
                catch(System.IO.FileNotFoundException e)  
                {  
                    // Handle the exception and proceed.  
                }  
                catch(Exception e)  
                {  
                    // Handle the exception (if you can) or close the application in a graceful manner.  
                }  
                finally 
                {  
                    // Resources Clean up (if any) ...  
                }  
     
                if(obj != null)  
                {  
                    // Do something.
                    obj.ToString(); 
                } 

    Regards.
    • Edited by Eyal-Shilony Wednesday, June 18, 2008 3:27 PM Correction.
    Wednesday, June 18, 2008 3:22 PM
  • I did try to use to the Try Catch, but I can't try on the line in which I'm Instantiate an XpathDocument using the reader. 

    Any other suggestions?

    Nicole
    Wednesday, June 18, 2008 3:29 PM
  • Hmm ... sorry, but it seems that there's a broken communication here.

    Either you're not explaning the problem at hand in details or I fail to understand it.

    If I understand you correctly The XPathDocument doesn't find the given file hance resulting the exception.

    Now I can't really understand why as I don't have enough details to work with to monitor the problem.

    You should give some "snapshot" of the code, farther details, or output to resolve the problem.

    Giving the the exception and the line the error is being raised is insufficient to help you in this case, sorry.

    Regards.
    .Net developer and software consultant.
    • Edited by Eyal-Shilony Wednesday, June 18, 2008 4:29 PM Some info.
    Wednesday, June 18, 2008 4:28 PM
  • Not a problem, I will try to explain better. 

    I am writing a program to receive an XML file from the user.  This XML file is exported out of Test Track Pro (the company's Defect Tracking Database).  The first lines of the XML file that is exported is as follows:

                <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                <!DOCTYPE TestTrackData SYSTEM "TestTrackData.dtd">
                <TestTrackData>

                <defect>


    Under the defect node is just more information on the defects.  The Doctype DTD line is the issue.  The following code is the loading of the XML file, into a text reader, and then using xpath to navigate.



    private void btnLoadXML_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.Visible = false;
                    XmlDocument Xmlfile = new XmlDocument();

                    if (openFileDialog1.CheckFileExists)
                    {
                      
                        //Load XML into reader
                        XmlTextReader reader = new XmlTextReader(openFileDialog1.FileName);
                      
                        //Instantiate an XPathDocument using the reader
                        XPathDocument xpathdoc = new XPathDocument(reader);
      

                        //get the navigator
                        XPathNavigator xpathnav = xpathdoc.CreateNavigator();



    When running my code (as it is above) I received "FileNotFoundexception was unhandled" "Could not find file testtrackdata.dtd"  This error has "XPathDocument xpathdoc = new XPathDocument(reader);" highlighted as the source of the issue.


    If I write in
    reader.ProhibitDTD = true; (before "XPathDocument xpathdoc = new XPathDocument(reader);")

    when running the code, it stops at the same line stating that DTD files are prohibited. 


    I would like to write a try catch, but I can't get it to work.


    try {
    XPathDocument xpathdoc = new XPathDocument(reader);
    }

    catch(System.IO.FileNotFoundException e)   ***I don't know what the 'e' is for**

    {}  //I'd like it to do nothing just continue reading the file



    This try catch doesn't work, because I receive an error (can't even run debug) that xpath does not exsist.  This is because I create xpathdoc within the try.


    So I'm good with either finding a way to get this try catch to work, or I'm looking for a way to get into the XML file and deleting that DTD line.  (I did this manually and the XML file works fine with my program).  But I'm rather fine a way for the program to do it, then to have to make it another step for the user to do. 

    Any suggestions?

    And thank you for your help with my issue, I do appreciate your time.  If you need me to clear up anything else, just ask. 

    Thanks!


    Nicole
    Wednesday, June 18, 2008 4:50 PM
  • FYI

    All I needed to do to get past those Exception errors was to put in the following code

    reader.XmlResolver = null;

    I put this code in after loading the XML into reader and before instantiate an XpathDocument using the reader.

    Thanks again for your time!

    Nicole
    Wednesday, June 18, 2008 5:11 PM
  •  TestTrackData.dtd

    Could it be relative vs absolut path issue? Where is this DB located? Perhaps your app has a default pathway to look for the resources and this DB (or table) is elsewhere? This is why most frequently such problems occur.
    AlexB
    Wednesday, June 18, 2008 10:10 PM
  • Discussion in Data Platform Development


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, June 20, 2008 9:38 AM
  • This thread was moved into the Off Topic forum. I can move it into a specific forum for you if you want.

    Thanks!


    Ed Price (a.k.a User Ed), SQL Server Experience Program Manager (Blog, Twitter, Wiki)

    Friday, May 11, 2012 4:55 AM