2013-03-17 4 views
-3

мне нужно разобрать его, чтобы получить элементы в PHP я просто использовать простой файл XML, но это возможно с помощью C#Чтение XML с помощью C#

<answers> 
<answer ID="START"> 
<text>VVLV?</text> 
<id>0</id> 
<responses> 

<response id="1"> 
<key>!1</key> 
<product> 123123 </product> 
<command>goto ANSWER_1</command> 
</response> 

<response id="2"> 
<key>!2</key> 
<product> 213sad </product> 
<command>goto ANSWER_2</command> 
</response> 

<response id="3"> 
<key>!3</key> 
<product>dfbdfgsdf </product> 
<command>goto ANSWER_3</command> 

</response> 

</responses> 

</answer> 

<answer ID="ANSWER_1"> 
<text>dfbdfgb?</text> 
<id>1</id> 
<responses> 

<response id="1"> 
<key>!1</key> 
<product> 123 </product> 
<command>check_product 1 goto ANSWER_9 ANSWER_11</command> 
</response> 

<response id="2"> 
<key>!2</key> 
<product> 321 </product> 
<command>check_product 1 goto ANSWER_9 ANSWER_12</command> 
</response> 

<response id="3"> 
<key>!3</key> 
<product> asd 3 </product> 
<command>check_product 1 goto ANSWER_9 ANSWER_11</command> 

</response> 

</responses> 

</answer> 


<answer ID="ANSWER_2"> 
<text>asd?</text> 
<id>2</id> 
<responses> 

<response id="1"> 
<key>!1</key> 
<product> ads </product> 
<command>goto ANSWER_4</command> 
</response> 

<response id="2"> 
<key>!2</key> 
<product> asdasd </product> 
<command>goto ANSWРІER_2</command> 
</response> 

<response id="3"> 
<key>!3</key> 
<product> dscdsc</product> 
<command>goto ANSWER_3</command> 

</response> 

</responses> 

</answer> 

<answer ID="ANSWER_3"> 
<text>asdasd</text> 
<password>1</password> 
<id>3</id> 
<responses> 

<response id="1"> 
<key>!1</key> 
<product> asdasd </product> 
<command>goto ANSWER_0</command> 
</response> 

<response id="2"> 
<key>!2</key> 
<product> </product> 
<command>goto ANSWER_2</command> 
</response> 

<response id="3"> 
<key>!3</key> 
<product> Testqdawd 3 </product> 
<command>goto ANSWER_3</command> 

</response> 

</responses> 

</answer> 

Как я могу разобрать его в C#, например, как я могу получить затухание elent в ответ с ID = «START» -> ответы-> ответ с ключом id 3->

+2

Как насчет RTFM? На MSDN много материала. –

+1

Читать дальше [LINQ To XML] (http://msdn.microsoft.com/en-us/library/bb387098.aspx) –

+3

Сообщество здесь не любит вопросы, которые показывают отсутствие исследований и/или усилий. Этот вопрос показывает такой недостаток. – Oded

ответ

1

Вы должны прочитать о LINQ to XML. И вы определенно должны были это сделать, прежде чем задавать какой-либо вопрос здесь!

Используя XDocument.Load метод, вы можете легко загрузить XML в XDocument объекта:

var dox = XDocument.Load("Input.txt"); 

А затем запросить его с помощью стандартного LINQ синтаксиса XML:

var response = (from a in dox.Root.Elements("answer") 
       where (string)a.Attribute("ID") == "ANSWER_1" 
       from r in a.Element("responses").Elements("response") 
       where (int)r.Attribute("id") == 1 
       select r).FirstOrDefault(); 

Или XPath селектор:

var response = dox.XPathSelectElement("answers/answer[@ID='ANSWER_1']/responses/response[@id='1']"); 
Смежные вопросы