Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8233759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:14:25+00:00 2026-06-07T18:14:25+00:00

I developed a WPF application using XML as the database file. Yesterday, the program

  • 0

I developed a WPF application using XML as the database file. Yesterday, the program stopped working. After some checking, I saw that there was a problem with Transaction.xml file. I tried opening the same in IE, but got this error

The XML page cannot be displayed

Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


An invalid character was found in text content. Error processing resource ‘file:///C:/RegisterMaintenance/Transaction.xml

Then, I tried opening the file in notepad and it showed weird character(screenshot below).

enter image description here

In the end, its displaying the right structure of xml. Please tell me what has gone wrong and why the xml not showing correctly. How can get it to normal state. I am really worried as this is my only data file. Any help or suggestion will be great.

One of the codes that edit this file, there are other similar types of code files that use Transaction.xml

public string Add()
    {
        XDocument doc1 = XDocument.Load(@"Ledgers.xml");
        XElement elem = (from r in doc1.Descendants("Ledger")
                where r.Element("Name").Value == this.Buyer
                select r).First();
        this.TinNo = (string)elem.Element("TinNo");
        this.PhoneNo = (string)elem.Element("PhoneNo");
        this.CommissionAmount = (this.CommissionRate * this.Amount) / 100;
        this.CommissionAmount = Math.Round((decimal)this.CommissionAmount);
        this.VatAmount = (this.CommissionAmount + this.Amount) * this.VatRate / 100;
        this.VatAmount = Math.Round((decimal)this.VatAmount);
        this.InvoiceAmount = this.Amount + this.CommissionAmount + this.VatAmount;
        XDocument doc2 = XDocument.Load(@"Transactions.xml");
        var record = from r in doc2.Descendants("Transaction")
                     where (int)r.Element("Serial") == Serial
                     select r;
        foreach (XElement r in record)
        {
            r.Element("Invoice").Add(new XElement("InvoiceNo", this.InvoiceNo), new XElement("InvoiceDate", this.InvoiceDate),
                new XElement("TinNo", this.TinNo), new XElement("PhoneNo", this.PhoneNo), new XElement("TruckNo", this.TruckNo), new XElement("Source", this.Source),
                new XElement("Destination", this.Destination), new XElement("InvoiceAmount", this.InvoiceAmount),
                new XElement("CommissionRate", this.CommissionRate), new XElement("CommissionAmount", this.CommissionAmount),
                new XElement("VatRate", this.VatRate), new XElement("VatAmount", this.VatAmount));
        }
        doc2.Save(@"Transactions.xml");
        return "Invoice Created Successfully";
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:14:26+00:00Added an answer on June 7, 2026 at 6:14 pm

    C# is an Object Orient Programming (OOP) language, perhaps you should use some objects! How can you possibly test your code for accuracy?

    You should separate out responsibilities, an example:

    public class Vat
    {
        XElement self;
        public Vat(XElement parent)
        {
            self = parent.Element("Vat");
            if (null == self)
            {
                parent.Add(self = new XElement("Vat"));
                // Initialize values
                Amount = 0; 
                Rate = 0;
            }
        }
    
        public XElement Element { get { return self; } }
    
        public decimal Amount
        {
            get { return (decimal)self.Attribute("Amount"); }
            set
            {
                XAttribute a = self.Attribute("Amount");
                if (null == a)
                    self.Add(new XAttribute("Amount", value));
                else
                    a.Value = value.ToString();
            }
        }
    
        public decimal Rate
        {
            get { return (decimal)self.Attribute("Rate"); }
            set
            {
                XAttribute a = self.Attribute("Rate");
                if (null == a)
                    self.Add(new XAttribute("Rate", value));
                else
                    a.Value = value.ToString();
            }
        }
    }
    

    All the Vat data will be in one node, and all the accessing of it will be in one testable class.

    Your above foreach would look more like:

    foreach(XElement r in record)
    {
        XElement invoice = r.Add("Invoice");
        ...
        Vat vat = new Vat(invoice);
        vat.Amount = this.VatAmount;
        vat.Rate = this.VatRate;
    }
    

    That is readable! At a glance, from your code, I cannot even tell if invoice is the parent of Vat, but I can now!

    Note: This isn’t to say your code is at fault, it could be a hard-drive error, as that is what it looks like to me. But if you want people to peruse your code, make it readable and testable! Years from now if you or someone else has to change your code, if it isn’t readable, it is useless.

    Perhaps from this incident you learned two things

    1. read-ability and test-ability.
    2. Backups! (All my valuable Xml files are in a SVN (TortoiseSVN) so I can compare what has changed, as well as keeping good backups. The SVN is backed-up to online storage.)

    An ideal next step is to take the code in the property setters and refactor that out to a static function extension that is both testable and reproducable:

    public static class XAttributeExtensions
    {
        public static XAttribute SetAttribute(this XElement self, string name, object value)
        {
            // test for correct arguments
            if (null == self)
                throw new ArgumentNullException("XElement to SetAttribute method cannot be null!");
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("Attribute name cannot be null or empty to SetAttribute method!");
            if (null == value) // how to handle?
                value = ""; // or can throw an exception like one of the above.
    
            // Now to the good stuff
            XAttribute a = self.Attribute(name);
            if (null == a)
                self.Add(a = new XAttribute(name, value));
            else
                a.Value = value.ToString();
            return a;
        }
    }
    

    That is easily testable, very readable and the best is it can be used over and over again getting the same results!

    Example, the Amount property can be greatly simplified with:

    public decimal Amount
    {
        get { return (decimal)self.Attribute("Amount"); }
        set { self.SetAttribute("Amount", value); }
    }
    

    I know this is a lot of boiler-plate code, but I find it readable, extendable and best of all test-able. If I want to add another value to Vat, I can just modify the class and not have to worry about have I added it in the right place. If Vat had children, I’d make another class that Vat had a property for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've developed a desktop application using .NetFX3.5 which has some winforms and two WPF
We are developing a customized installer using Wix and Wpf. We have developed some
I have developed a GUI for a random application using WPF. I have a
I'm writing tests using WiPFlash for a WPF application that has been developed in
I'm currently working on an client application developed using winforms on .Net 2.0. Are
I have developed a WPF application. I have a one resource dictionary file in
I'm developing a graph control in WPF. I have previously developed it using GDI
I recently built a new application using WPF, so that I can learn the
I have developed a simple DB-editing app using Xceed's excellent DataGrid for WPF (UX
I have some applications developed in asp.net, WPF ,windows Form etc.How can i run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.