1

Im trying to read a xml file with the code as below.

<?xml version="1.0" encoding="utf-8" ?>

<files>
   <pdf_input infolder ="C:\Users\Lambo\Documents\Visual Studio 2010\Projects\test\test\testPdfIn"
              outfolder ="C:\Users\Lambo\Documents\Visual Studio 2010\Projects\test\test\testPdfOut"
              autonameappend="_new" />


   <word_file infolder =" C:\Users\Lambo\Documents\Visual Studio 2010\Projects\test\test\testPdfIn"
              outfolder ="C:\Users\Lambo\Documents\Visual Studio 2010\Projects\test\test\testPdfOut" />


   <pdf_file fileRequired="true" directory="" autonameappend="pdf" />


   <docx_file fileRequired="true" directory="" autonameappend="docx" />

   <!-- autonameappend: Such as: (copy) -->


   <doc_file fileRequired="true" removePicture="true" removeFormfield="true"  directory="" autonameappend="_new" />


</files>

but some how the Im not able to read it. here is the code which im using to try to read the xml file.

public static void readConfig()
{
    try
    {
     //   StreamReader sr = new StreamReader("");
        XmlTextReader reader = new XmlTextReader("~/bin/config.xml");



        reader.MoveToContent();

        reader.ReadToDescendant("pdf_input");

        pdf_infolder = reader.GetAttribute("infolder");

        pdf_outfolder = reader.GetAttribute("outfolder");

        pdf_nameAppend = reader.GetAttribute("autonameappend");

        MessageBox.Show("two passed");



        word_outfolder = reader.GetAttribute("outfolder");          

        reader.ReadToNextSibling("pdf_file");
        pdf_required = Convert.ToBoolean(reader.GetAttribute("fileRequired"));
        pdf_newDirectoryV=reader.GetAttribute("directory");
        pdf_autoName = reader.GetAttribute("autonameappend");

        MessageBox.Show("3 passed");

        reader.ReadToNextSibling("docx_file");
        docx_required = Convert.ToBoolean(reader.GetAttribute("fileRequired"));
        docx_newDirectoryV=reader.GetAttribute("directory");
        docx_autoName = reader.GetAttribute("autonameappend");

        MessageBox.Show("4 passed");

        reader.ReadToNextSibling("doc_file");
        doc_required = Convert.ToBoolean(reader.GetAttribute("fileRequired"));
        doc_removePic = Convert.ToBoolean(reader.GetAttribute("removePicture"));
        doc_removeFF = Convert.ToBoolean(reader.GetAttribute("removeFormfield"));
        doc_newDirectoryV=reader.GetAttribute("directory");
        doc_autoName = reader.GetAttribute("autonameappend");

        reader.Close();

    //   MessageBox.Show("Success");

    //   MessageBox.Show("pdf_required is :" + pdf_required + "        pdf_newdirectory is :" + pdf_newDirectoryV + "End");

    }
    catch (Exception)
    {

        MessageBox.Show("reading config file failed, using default value instead" );
        restoreDefault();
    }
}

private static void restoreDefault()
{

  //  wordName = @"C:\Users\user\Documents\Visual Studio 2010\Projects\SecureWord\SecureWord\bin\Debug\Sample3.doc";
    pdf_required = true;
    pdf_newDirectoryV = "";
    pdf_autoName = "";

    docx_required = true;
    docx_newDirectoryV = "";
    docx_autoName = "";

    doc_required = true;
    doc_removePic = true;
    doc_removeFF = true;
    doc_newDirectoryV = "";
    doc_autoName = "";

}

}

Anyhelp would be much appreciated Thanks alot!

2
  • 2
    How are you "not able to read it"? Commented Apr 24, 2012 at 17:32
  • It's giving me a ArgumentNullException when im trying to get the path of the infolder. Commented Apr 25, 2012 at 1:19

4 Answers 4

4
new XmlTextReader("~/bin/config.xml")

The tilde path (officially, "Web Application root operator" only works for server controls and other ASP.NET aware utilities - not anywhere a path is needed.

You can use Server.MapPath to get a physical location for the file.

new XmlTextReader(Server.MapPath("~/bin/config.xml"))
Sign up to request clarification or add additional context in comments.

4 Comments

The file is a .CS file so i don't think i'll be able to use Server.MapPath in this case
Assuming you're running the .cs in ASP.NET, use HttpContext.Current.Server.MapPath
What if the .cs is a console application, and im trying to get it read the xml file. and then use ASP.NET to call the cs file that reads the xml file
@SamuelTang - in a console app, you'd need to resolve the path yourself. Assuming the XML file is located next to the .EXE, you could just use "config.xml".
1

You likely need to convert the relative ASP.NET path to a physical path. See HttpServerUtility.MapPath.

XmlTextReader reader = new XmlTextReader(Server.MapPath("~/bin/config.xml"));

Comments

0

I never used a XmlTextReader, go with this instead it seems alot easier to work with.

XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml("~path/mydoc.xml");
        foreach (XmlNode xNode in xDoc.ChildNodes)
        {
            //Do w.e
        }

Comments

0

Linq2Xml is easier to use

XDocument xDoc = XDocument.Load(....);
var dict = xDoc.Element("files")
    .Descendants()
    .ToDictionary(k=>k.Name,v=>v.Attributes().ToDictionary(ak=>ak.Name,av=>av.Value));

foreach (var item in dict)
{
    Console.WriteLine(item.Key+":");
    foreach (var attr in item.Value)
    {
        Console.WriteLine("\t"+attr.Key + "="+ attr.Value);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.