0

I'm trying to write a method who takes a xml.config file from some location in file system via Load button, parse it and after that do work with specific strings in element attributes type and mapTo, getting some substrings from those attributes. The problem is that instead of getting strings in table I get some lines. Anyone knows what can be the problem?

Button and method call looks like this:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();

            fDialog.Title = "Open XML file";
            fDialog.Filter = "XML files|*.config";
            fDialog.InitialDirectory = @"C:\";

            bool? control = fDialog.ShowDialog();
            if (control.Value)
            {
                var filePath = fDialog.FileName;
                ReadAdvancedConfigFile(filePath);
            }

        }

Method that gets values from xml file, take substring for each attribute type and mapTo, put them into two separated lists and try to write content of list into a datagrid looks like this:

private void ReadAdvancedConfigFile(string path)
{
    XElement root = null;
    root = XElement.Load(new XmlTextReader(path));

    if (root != null)
    {
        XNamespace ns = "http://schemas.microsoft.com/practices/2010/unity";
        var registers = root.Element(ns + "unity").Element(ns + "container").Descendants(ns + "register");

        if (registers.Count() > 0)
            {
            var tipList = registers.Select(x => x.Attribute("type").Value);
            var mapToList = registers.Select(x => x.Attribute("mapTo").Value);
            List<string> listresult = new List<string>();
            List<string> listresultm = new List<string>();

                foreach (string tpl in tipList)
                {
                    int end = tpl.IndexOf(',');
                    int start = tpl.LastIndexOf('.', (end == -1 ? tpl.Length - 1 : end)) + 1;
                    string result = tpl.Substring(start, (end == -1 ? tpl.Length : end) - start);
                    listresult.Add(result);
                }
                foreach (string mpl in mapToList)
                {
                    int endm = mpl.IndexOf(',');
                    int startm = mpl.LastIndexOf('.', (endm == -1 ? mpl.Length - 1 : endm)) + 1;
                    string resultm = mpl.Substring(startm, (endm == -1 ? mpl.Length : endm) - startm);
                    listresultm.Add(resultm);
                }

                int maxLenList = Math.Max(listresult.Count, listresultm.Count);
                for (int i = 0; i < maxLenList; i++)
                {
                    if (i < listresult.Count && i < listresultm.Count)
                    {
                        _obsCollection.Add(new Tuple<string, string>(listresult[i], listresultm[i]));
                    }
                    else if (i >= listresult.Count)
                    {
                        _obsCollection.Add(new Tuple<string, string>(string.Empty, listresultm[i]));
                    }
                    else if (i >= listresultm.Count)
                    {
                        _obsCollection.Add(new Tuple<string, string>(listresultm[i], string.Empty));
                    }
                }
            tabela.ItemsSource = _obsCollection;
        }
    }
} 

Content of XML.config file look like this:

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

    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
    </configSections>

    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

        <container name="container">

            <register name="configService" type="Web.Common.Interfaces.IConfigService, Web.Common"
                      mapTo="Web.Common.Services.ConfigServiceImpl, Web.Common">
                <lifetime type="singleton" />
                <constructor>
                    <param name="res" value="Resources.ClientStrings"> </param>
                    <param name="configFile" value="webclient.config"> </param>
                </constructor>
                <!--<property name="LocalisationService" dependencyName="LocalisationService" />-->
                <!--This is a property injection from the language plugin -->
            </register>

            <register name="scaleCoefConfigService" type="Web.WebClient.Services.IScaleCoefConfigService, Web.WebClient.TDMSWebApp"
                      mapTo="Web.WebClient.Services.Implementations.ScaleCoefConfigServiceImpl, Web.WebClient.TDMSWebApp">
                <lifetime type="singleton" />
                <constructor>
                    <param name="configService">
                        <dependency name="configService"/>
                    </param>
                </constructor>
            </register>

            <register name="sessionService" type="Web.Common.Interfaces.ISessionService, Web.Common" 
                      mapTo="Web.Common.Services.SessionServiceImpl, Web.Common">
                <lifetime type="singleton" />
            </register>

            <register name="licenseManagerService" type="Web.Common.Interfaces.ILicenseManagementService, Web.Common"
                      mapTo="Web.Common.Services.LicenseManagementServiceImpl, Web.Common">
                <lifetime type="singleton" />
            </register>
        </container>
    </unity>
</configuration>

And this is how I've created Tuple for string pair. In this class is also definition for ReadAdvancedConfigFile method and Load button, of course:

    public partial class CreateAreaDialogWindow : System.Windows.Window
        {
            ObservableCollection<Tuple<string, string>> _obsCollection = new ObservableCollection<Tuple<string, string>>();

            public CreateAreaDialogWindow()
            {
                InitializeComponent();
            }
}

XAML code for datagrid is this:

<DataGrid AutoGenerateColumns="False" Height="146" HorizontalAlignment="Left" Margin="34,275,0,0" Name="tabela" VerticalAlignment="Top" Width="384" SelectionChanged="tabela_SelectionChanged" />

Hope I didn't get this too complicated.

5
  • Can you add a screenshot of the problem. It isn't all clear to me. Commented Feb 5, 2014 at 9:16
  • @PatrickHofman screenshot of datagrid with lines? Or something else? Commented Feb 5, 2014 at 9:19
  • Yes, that is what I meant. Commented Feb 5, 2014 at 9:25
  • @PatrickHofman Won't send a picture for some reason... I will continue to try. But it's a basically Window with that one Load button and DataGrid bellow it. In DataGrid I have lines instead of strings in two columns. I think that problem may be with this line: tabela.ItemsSource = _obsCollection; Cause I'm not sure can ItemsSource can work like that? Commented Feb 5, 2014 at 9:32
  • I got it. I saw the issue myself, but it was easier to tell with an image. Don't worry about it any more. See my answer. Commented Feb 5, 2014 at 9:32

1 Answer 1

1

You have set AutoGenerateColumns="False" and you provided no columns.

Try this and it works:

<DataGrid AutoGenerateColumns="True" />

Or see MSDN on how to create columns.

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

2 Comments

I love u man! :D It worked! :) Thanks a lot. I know that this been stupid, but I'm still learning and that can be tough :)
And thanks for the link. I will need it :) Cause next I have to research how to add checkboxes into that tabel for each value :)

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.