powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / чтение из xml файла
4 сообщений из 4, страница 1 из 1
чтение из xml файла
    #37107309
eugenia_
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Здравствуйте!
начинаю изучение WPF и возникла следующая проблема -

есть XML файл -
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
     
 
<?xml version="1.0" encoding="utf-8" ?>
 
<HEAD>
        
        <MenuPosition>Pasta</MenuPosition>
        
        
        <Pasta>
        
        
        
        <Item1>
                <Name>"Pasta Fra Diavolo"</Name>
                        <About>"Pasta with Red Spicy Sauce"</About><Ingridients>
                        "Pasta, small shrimps,scallops, olive oil, garlic, chopped tomatoes, salt, crushed red pepper,
                         chopped fresh parsley "</Ingridients>
        </Item1>
 
        <Item2>
        <Name>"Spaghetti with Tomato Sauce "</Name><About>"Pasta with red non-spicy sauce"</About>
                <Ingridients>
                        "Spaghetti,lean ground beef, salt, white sugar, dried oregano, ground black pepper, garlic powder,
                        dried minced onion, chopped tomatoes, tomato paste, sliced mushrooms"</Ingridients>
   </Item2>
        
        <Item3><Name>"Pasta Fettuccine Alfredo"</Name><About>"Pasta with heavey cream sauce"</About>
                <Ingridients>
                        "fettuccine, milk, butter, cream cheese, Parmesan cheese, white pepper"
        </Ingridients>
                </Item3>
 
        <Item4><Name> "Pasta with white clam sauce" </Name><About> "Pasta with light cream sauce" </About>
        <Ingridients>
                "pasta, chopped onion, white clam sauce, olive oil, lemon juice, minced garlic, crushed red pepper"
        </Ingridients>
</Item4>
                </Pasta>
 
 
 
 
        <MenuPosition>Meat</MenuPosition>
        <Meat><Item1><Name>"Beef Curry"</Name><About>"Spicy red meat"</About>
        <Ingridients>
                "beef stew meat, olive oil, salt, pepper, garlic, chopped fresh ginger, jalapeno peppers, curry powder, 
                tomatoes with juice, onion"
        </Ingridients>
                </Item1>
                
        
        <Item2><Name>
                "Pork Medallions"</Name><About>"Non spicy red meat" </About><Ingridients>
                        "pork tenderloin, olive oil, butter, onion, mushrooms, garlic, chicken broth,
                        dried rosemary, dried savory, salt, pepper, fresh parsley"
                </Ingridients>
        </Item2>
        </Meat>
 
 
 
 
 
        <MenuPosition>Fowl</MenuPosition>
        <Fowl><Item1><Name> "Fried Chicken" </Name><About> </About>
        <Ingridients>
                " chicken breast, saltine crackers, flour, potato flakes, seasoned salt, ground black pepper,
                egg, vegetable oil"
        </Ingridients>
                </Item1>   
                
                <Item2><Name>"Baked Ziti Turkey"</Name><About>"Light meat fowl"</About>
<Ingridients>
        "turkey breast, butter, dried thyme, dried rosemary, paprika, garlic powder, tenderloin"
</Ingridients>
                </Item2>
                
                <Item3><Name>"Peking Duck"</Name><About>"Dark meat fowl"</About>
                <Ingridients>
                        "duck, ground cinnamon, ground ginger,ground nutmeg,ground white pepper,
                        ground cloves, soy sauce, honey, orange, chopped fresh parsley, 
                        green onions, plum jam, sugar, white vinegar, chopped chutney"
                </Ingridients>
            </Item3>
                </Fowl>
 
 
 
        <MenuPosition>Fish</MenuPosition>
        <Fish><Item1><Name>"Tuna*and Salad of Mixed Greens with Wasabi Vinaigrette" </Name>
        <About>"Non-bland fish"</About><Ingridients>
                "tuna*steak, Montreal Steak Seasoning, salt, coarse pepper, vegetable oil, salad greens, 
            radishes, cucumber, wasabi paste, rice vinegar, soy sauce, olive oil, ground black pepper"
        </Ingridients>
        </Item1>
                
                
                <Item2><Name>"Flavorful Flounder"</Name><About>"Bland fish"</About>
                <Ingridients>
                        " flounder fillets, lemon juice, grated Parmesan cheese, butter, mayonnaise, chopped green onions,
                        salt"
                </Ingridients>
</Item2>
        </Fish>
 
 
 
 
        <MenuPosition>Shellfish</MenuPosition>
        <Shellfish>
                <Item1><Name>"Fried crab with salad" </Name><About>"Non-oyster shellfish"</About>
                        <Ingridients>
                                "Crab, salad, lemon, salt, black pepper"
                        </Ingridients>
                        </Item1>
                
                <Item2> <Name> "Backed lobster" </Name> <About>"Non-oyster shellfish"   </About> 
                <Ingridients>
                        "Lobster, butter, salad, tomatoes, salt, black pepper"
                </Ingridients>
                
                </Item2>
                
                <Item3><Name>"Backed oysters"</Name><About>"Oyster shellfish"</About>
                <Ingridients>
                        "shell-on oysters, garlic, chopped parsley leaves, butter, salt, paprika, lemon wedges"
                </Ingridients>
                        </Item3>
                
                </Shellfish>
 
 
        <MenuPosition>Other food</MenuPosition>
                <Pizza>
                        <Item1><Name>"Vegetable Pizza"</Name><About>"Vegetarian Pizza"</About>
                        <Ingridients>
                                "flour, milk, eggs,salt,
                                 mayonnaise, cream cheese, dill weed, squash, mushrooms, carrots, radishes, cucumber, ripe olives,
                                shredded Cheddar cheese, mozzarella cheese"  </Ingridients>
                        
                        </Item1>
                <Item2><Name>
                        "Supreme Bagel Pizza"</Name><About>"Non-vegetarian Spicy Pizza" </About>
                        <Ingridients>
                                "flour, milk, eggs, salt, bagels, pizza sauce, pepperoni, ham,
                                bacon bits, chopped green pepper, mozzarella cheese "
                        </Ingridients>
                        </Item2>        
                        
                        <Item3>
                                <Name>"Mediterranean Pizza"</Name><About> "Non-vegetarian Spicy Pizza"  </About>
                                <Ingridients>
                                        " Flour, milk, eggs, salt, artichoke hearts, dried basil, dried oregano, dried thyme,
                                        shredded Monterey Jack cheese, ham, cherry tomatoes, ripe olives, feta cheese"
                                </Ingridients>
                        </Item3>
                        </Pizza>
 
 
 
        <MenuPosition>Desserts</MenuPosition>
                <Dessert><Item1><Name>" Fruit dessert" </Name><About>"Sweet fruit dessert"</About>
                <Ingridients>"Pineapple, mango, pomegranate, strawberries, grapes, clementine, apricot, banana"</Ingridients>
            </Item1>
                        
                        <Item2><Name>"Cheese nuts cake"</Name><About>"Sweet dessert" </About> 
                        <Ingridients>
                                "sugar, butter, eggs, bananas, buttermilk, vanilla, flour, whole wheat flour, baking soda, salt,
                                nutmeg, walnuts, cream cheese"
                        </Ingridients>
                        </Item2>
                        </Dessert>
        
        
        
        
        <TodaysSpecial>
                <Item1><Name>"Pizza Carbonara"</Name><About>"from Cheff Mario"</About>
                <Ingridients>" Pizza crust, chopped onion, garlic, butter, flour, white pepper, milk,
                 chicken bouillon, grated Parmesan cheese, sliced bacon, shredded Monterey Jack cheese, green onions"
                </Ingridients>
        </Item1>
                </TodaysSpecial>
 
</HEAD> 

Есть окно , на котором есть Комбо-бокс и лист-бокс.

Нужно считаь данные их этого XML файла, например, в комбо бокс.
Например, то, что стоит в
Код: plaintext
1.
<MenuPosition>Pasta....</MenuPosition>
нужно добавить в комбобокс. Все это должно сделаться при загузке окна -
Код: plaintext
1.
 <ComboBox IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Name="MenuPos" Loaded="FillMyListBox" Margin="8,95,0,0" VerticalAlignment="Top" Width="260" Height="36"/>   
  
Вот мой код поиска и добавления элементов -
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
 
 private void FillMyListBox(object sender, EventArgs e)
        {
            XmlTextReader reader = new XmlTextReader("MenuFood.xml");
 
 
            XmlNodeType type;
 
            while (reader.Read()) {
                type = reader.NodeType;
                if (type == XmlNodeType.Element) { 
                if (reader.Name=="MenuPosition"){
                    reader.Read();
 
                    MenuPos.Items.Add(reader.Value);
 
 
 
                    }
                
                
                }
            
            
            
            }
 
            reader.Close();
        
        
        }
Когда я запускаю приложение, то оно зависает.
Подскажите, пожайлуста, в чем проблема.

У меня еще второй вопрос -
кроме элементов, стоящих в <MenuPosition>
нужно считать еще и SubElements.
К примеру, в <MenuPosition> стоит "Pasta". Потом дальше идут разновидности этого элемента , допустим, Pasta yyyy, Pasta xxxxxx, Pasta zzzzzz.....и так далее, стоящие в тегах <Item1>,<Item2>,<Item3>
Подкиньте, пожайлуста, идею, как это релизовать? Если в комбо бокс стоит "Pasta", то в лист боксе должно стоять Pasta yyyyyyy, Pasta xxxxxx, Pasta zzzzzzzzz.
Нужен ли второй ридер?
спасибо за ответы
...
Рейтинг: 0 / 0
чтение из xml файла
    #37107423
AlexeiK
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
наверное это вложенный вызов read.
деталей как этот ридер реализован не знаю, но все может быть.
...
Рейтинг: 0 / 0
чтение из xml файла
    #37107450
AlexeiK
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
вообще лучше сгенерить класс под этот xml и потом использовать десериализацию.
...
Рейтинг: 0 / 0
чтение из xml файла
    #37107870
Фотография уТКа
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
вообще автор по какой-то не известной причине парсит XML руками, вместо того чтобы воспользоваться LINQ
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / чтение из xml файла
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]