I think this is some kind of coding problem

I have two computers. Both run WinXP SP2 (I really don't know how similar they are). I am running MS Visual C # 2008 Express Edition for both and what I am currently using for programming.

I made an application that loads into an XML file and displays the content in a DataGridView.

First line of my xml file:

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

      

... and indeed ... it's utf-8 (at least according to MS VS C # when I just open the file there).

I am compiling the code and running it on the same computer and the content of my DataGridView looks fine. No funny characters. I will compile the code and run it on another computer (or just ran the published version from computer # 1 and installed it on computer # 2 - I tried this both ways) and in the datagridview where there are line breaks / newlines in the xml file I I see funny square symbols.

I am new to coding ... so the only thing I was really trying to eliminate was to use this same program to write my xml content to a new xml file (but I am actually writing it to a text file with xml tags in it) as the default entry to a text file looks like utf-8. Then I read this new file in my program. I am getting the same results.

I don't know what else to do or how to fix this problem or what I am essentially wrong in the first place.

-Adeena

0


source to share


4 answers


I'm not sure about the cause of your problem, but one solution would be to simply strip out carriage returns from your lines. For each line you add, just call TrimEnd(null)

on it to remove trailing spaces:

newrow["topic"] = att1.ToString().TrimEnd(null);

      

If your lines might end up with other spaces (i.e. spaces or tabs) and you want to keep them, just pass an array containing only the carriage return character to TrimEnd

:



newrow["topic" = att1.ToString().TrimEnd(new Char[]{'\r'});

      

Disclaimer: I am not a C # programmer; the second statement may not be syntactically correct

+1


source


This is not related to UTF-8 encodings or characters - this issue is related to line endings . On Windows, each line of a text file ends with two carriage return (CR) and newline (LF, for line) characters, which are code points U + 000D and U + 000A, respectively. In ASCII and UTF-8, they are encoded as two bytes 0D 0A

. On the other hand, most non-Windows systems, including Linux and Mac OS X, only use the newline character to signal end-of-line, so it is not uncommon to have line-ending problems when transferring text files between Windows and non-Windows systems.



However, since you are only using Windows on both systems, this is more of a mystery. One application correctly interprets the CRLF combination as a newline, but another application confuses CR. A carriage return is not a printable character, so it replaces CR with the placeholder field that you see; it then correctly interprets the line feed as the end of the line.

+4


source


The square usually appears when you use different types of newlines.

  • Linux - (0A)

    LF
  • Win - (0D0A)

    CRLF
  • Mac - (0D)

    CR

The app was probably built using type 1 and the running app expects a different one.


Departure Environment.NewLine

And you can try this: (no guarantees - I don't write much C #)

strInput = Regex.Replace(strInput, "\\r?\\n?", Environment.NewLine)

      

+2


source


@ Adam: I'm sorry! Missed your previous expression.

To load a document into a program and display it in a DataGridView, I currently do (I say "currently" because I tried other things like using XDocument instead of Xelement):

XElement xe1 = XElement.Load(filePath);

DataTable myTable = new DataTable();
myTable = mkTable();   // calls a function that makes the table
var _categories = (from p1 in xe1.Descendants("category") select p1);
int numCat = _categories.Count();
int i = 0;

while (i < numCat)
{
    DataRow newrow;
    newrow = myTable.NewRow();

    if (_categories.ElementAt(i).Parent.Name == "topic")
    {
        string att1 = _categories.ElementAt(i).Parent.Attribute("name").Value.ToString();
        newrow["topic"] = att1.ToString();
    }
    // repeat the above for the different things in my document
    myTable.Rows.Add(newrow);

    i++;
}
myDataSet.Merge(myTable);
bindingSourceIn.DataSource = myDataSet;
myDataGridView.DataSource = bindingSourceIn;
myDataGridView.DataMember = "xmlthing";

      

(obviously shrinking a bit here ... i.e. my bindingsource / datagridview etc is declared elsewhere ... but hopefully that's enough to make sense)

-Adeena

0


source







All Articles