0
  • First off I want to start with the fact that I'm a begginer at coding , especialy C#.

  • Last year I've recived a program made in C# that could write XML elements to an excel file(at that point I didnt even know what kind of software).

  • I've just edited the program so that I could get diffrent information out of the XML files, but I've hit a wall.

  • I cannot seam to figure out how can I concatenate 2 identical elements of identical tags:

    <Person>
        <PERSONID>1</PERSONID>
        <FIRSTNAME>VICTOR</FIRSTNAME>
        <LASTNAME>DAN</LASTNAME>
    </Person>
    <Person>
        <PERSONID>2</PERSONID>
        <FIRSTNAME>JHON</FIRSTNAME>
        <LASTNAME>SMITH</LASTNAME>
    </Person>`
    

so that the output to be DAN VICTOR/SMITH JHON. Right now I've got just the first part

persoana = string.Concat(pp.LASTNAME, " ", pp.FIRSTNAME);

so when,

sout.WriteLine(string.Concat(new string[] { "\"", person, "\"" }));

kicks in, I get 2 rows in the .csv file for each person, but if I have multiple Tags with multiple Elements then the first one of the elemets get writen properly but the rest of them get updated with the last one , thats why I was thinkging if concatenating all of the elements.
- Another problem I've encountered is when I'm facing with XMLs that do not have certain elements , then I get a DBNull error, Ive tryed to add an if statement

if (! DBNull.Value.Equals(lr.PARCELLEGALAREA.ToString())){
    parcellegalarea = lr.PARCELLEGALAREA.ToString(); 
}

but it doesnt do the trick. The debugger keeps on showing me:

[DebuggerNonUserCode]
[GeneratedCode("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
public double PARCELLEGALAREA
{
    get
    {
        double item;
        try
        {
            item = (double)base[this.tableLand.PARCELLEGALAREAColumn];
        }
        catch (InvalidCastException invalidCastException)
        {
            throw new StrongTypingException("The value for column 'PARCELLEGALAREA' in table 'Land' is DBNull.", invalidCastException);
        }
        return item;
    }
    set
    {
        base[this.tableLand.PARCELLEGALAREAColumn] = value;
    }
}
10
  • Make DBNull value to string ToString() and then compare with == or !=. Commented Feb 16, 2018 at 9:07
  • One of the first tings Ive tried was if (lr.PARCELLEGALAREA.ToString()) == DBNull.ToString()) and if (lr.PARCELLEGALAREA.ToString()) == DBNull) but thats not a valid sintax. Could you be a little more specific? Also, thank you for your time Commented Feb 16, 2018 at 9:11
  • To verify that object is DBNull you have to compare it without ToString() - e.g. if (lr.PARCELLEGALAREA == DBNull.Value). The other method is to convert to string, then compare with empty string, not DBNull. Commented Feb 16, 2018 at 9:14
  • If you're looking for null specifically: if (lr.PARCELLEGALAREA == DBNull.Value). If you want to treat null and an empty string the same way: if (lr.PARCELLEGALAREA.ToString() == string.Empty). Commented Feb 16, 2018 at 9:15
  • The debbuger keeps on showing me System.Data.StrongTypingException: 'The value for column 'PARCELLEGALAREA' in table 'Land' is DBNull.' Commented Feb 16, 2018 at 9:21

1 Answer 1

1

be sure you don't have copies of the same cgxml in another folder inside the folder you select - thats the only case that made duplicte rows for the same person with me with your code because i had two copies of your example3.cgxml one in the parent directory and another one in subdirectory folder and i select the parent directory

You can use only files in your current directory by modifying this line

FileInfo[] files = (new DirectoryInfo(this.folderBrowserDialog1.SelectedPath))
.GetFiles("*.cgxml", SearchOption.TopDirectoryOnly);

instead of:

FileInfo[] files = (new DirectoryInfo(this.folderBrowserDialog1.SelectedPath))
.GetFiles("*.cgxml", SearchOption.AllDirectories);

According to your request you want to have row for each possible pacrel / person combination so your foreach percon loop need to be neasted into foreach parcel loop like this

foreach (CGXML.ParcelRow pr in fisier.Parcel)
                {
                    cat = pr.USECATEGORY;
                    measarea = pr.MEASUREDAREA.ToString();


                    foreach (CGXML.PersonRow pp in fisier.Person)
                    {
                        persoana = string.Concat(pp.LASTNAME, " ", pp.FIRSTNAME);
                        sout.WriteLine(string.Concat(new string[] { "\"", cadgenno.ToString(), "\",\"", sector, "\",\"", persoana, "\",\"", parcellegalarea.ToString(), "\",\"", measarea.ToString(), "\",\"", cat, "\"" }));

                    }
                }

and the output will be:

ID SECTOR Person ID Area Parcel Area Parcel Category

1 36 DAN VICTOR 7500 7000 A

1 36 DAN MARIA 7500 7000 A

1 36 DAN VICTOR 7500 500 F

1 36 DAN MARIA 7500 500 F

Sign up to request clarification or add additional context in comments.

3 Comments

Duplicationg xml files might happen only if some1 makes a copy of the XML folder in side it or make a edited version of an XML file in the same folder , Im mainly the only 1 who changes them and I do that on my PC not on the server so that wount be a problem. But thank you for the reminder I will add this to the notes so I can modify it later on if someone needs it that way.
I tried your program and produces 1 name per row except for that case and it didn't produce null exception too-so see whats the difference and check your server you can send another file that produce the problem so we can test it, thank you
I have 2 Parcels and 2 Persons in the XML file, the tabel output looks like this : Person 2 , Parcel 1 / Person 2 , Parcel 2 . Because the the Person is updated with the last Person. So Im looking for a difrent aproach , but I dont know how to make it so that I will get un output tat looks like : Person 1 | Person 2 , Parcel 1 / Person 1 | Person 2 , Parcel 2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.