2017-04-21 20 views
0
<Employee Id="01"> 
<Name> ABC </Name> 
<Telephone> 123456789</Telephone> 
<Age> 25</Age> 
<MartialStatus> False </MartialStatus> 
</Employee> 

<Employee Id="02"> 
<Name> XYZ </Name> 
<Telephone> 00000000</Telephone> 
<Age> 25</Age> 
<MartialStatus> False </MartialStatus> 
</Employee> 

電話番号を使用する特定の検索方法C#では、100人以上の従業員の詳細を持つXMLファイルが1つあり、従業員の詳細な電話番号を使用しているとします。どのように可能ですか?XMLの特定の要素を取得する方法

+0

あなたは試してみましたが、どこが立ち往生し、何が? –

+0

私はxmlファイルに複数の従業員の詳細を持っていますが、Cの電話番号を使用して特定の従業員の詳細を欲しいです。 –

+0

私の答えは今更新されました –

答えて

0

リストは、特定の電話番号を使用してofemployesたい場合は、この試すことができます:

XElement xmlDoc = XElement.Parse(xml); 
var employee = xmlDoc.Descendants("Employee").Where(x => x.Element("Telephone")?.Value == "123456789").ToList(); 

そして、ここでは、idのリストです:

List<string> employeeIDs = res.Select(x => x.Attribute("Id").Value).ToList(); 
0

従業員のためのコンテナとしてルートタグの従業員を作成します。

<Employees> 
 
    <Employee Id="01"> 
 
    <Name> ABC </Name> 
 
    <Telephone>123456789</Telephone> 
 
    <Age> 25</Age> 
 
    <MartialStatus> False </MartialStatus> 
 
    </Employee> 
 
    <Employee Id="02"> 
 
    <Name> XYZ </Name> 
 
    <Telephone>00000000</Telephone> 
 
    <Age> 25</Age> 
 
    <MartialStatus> False </MartialStatus> 
 
    </Employee> 
 
</Employees>

 // load file XDocument 
     XDocument _doc = XDocument.Load("C:\\t\\My File2.txt"); 
     /* 
     1. Select Employees 
      2. Select the Employee Element 
       3.Search int this Employee for elements with name "Telephone" 
        4.Extract the value and compare it to your given number 
      5. Continue to the next Employee to comaire 
     6.Select the first on of all the elements that for filled the search term 
     */  

     var employee = _doc.Element("Employees") 
      .Elements("Employee") 
      .Where(x => x.Element("Telephone")? 
      .Value == "00000000") 
      .FirstOrDefault(); 

     // Get values from elements of Employee 
     string name = employee.Element("Name").Value; 
     string age = employee.Element("Age").Value; 

     MessageBox.Show($"Name: {name}, Age {age}"); 
+0

私はすべての従業員の詳細をあなたのコード私は "もし"より多くの4または5時間を使用する必要がありますか? –

+0

あなたはそのようなユーザーの要素を得ることができますが、それはベストプラクティスではありません。私はそれがonelinerで行うことができるように私の質問を更新しました:) –

+0

@Vishal Prajapatiそれは動作しましたか? –

0

アプローチ使用したXPath

xml.xmlコンテンツ

<Employees> 
    <Employee Id="01"> 
     <Name> ABC </Name> 
     <Telephone> 123456789</Telephone> 
     <Age> 25</Age> 
     <MartialStatus> False </MartialStatus> 
    </Employee> 

    <Employee Id="02"> 
     <Name> XYZ </Name> 
     <Telephone> 00000000</Telephone> 
     <Age> 25</Age> 
     <MartialStatus> False </MartialStatus> 
    </Employee> 
</Employees> 

コード、それによって従業員にアクセスするための電話番号

string telNo = " 123456789"; 
XmlDocument doc = new XmlDocument(); 
doc.Load(@"c:\temp\xml.xml"); 
var employee = doc.SelectSingleNode("Employees/Employee[Telephone='"+ telNo + "']"); 
0

XMLへの使用のLINQ:

XElement xelement = XElement.Load(@"..\XMLfile1.xml"); 
      IEnumerable<XElement> employees = xelement.Elements(); 

      var elementYouNeed = employees.Where(x => x.Element("Telephone").Value.Trim() == "00000000"); 
      var nameYouNeed = elementYouNeed.ToList()[0].Element("Name").Value; 
関連する問題