0

here's my problem

I retrieve and display the xml data like this

 XDocument doc = XDocument.Load("TextFile1.xml");

            List<string> a = new List<string>();

            var kitap = doc.Descendants("Author");

            foreach (var item in kitap)
            {
                a.Add(item.Value);
            }
            list1.ItemsSource = a;

I have two different xml files such as;

<Books>
  <Book>
    <Author>Author1</Author>
  </Book>
  <Book> 
    <Author>Author2</Author>
  </Book>
</Books>

And the second one is like this;

 <Books>
    <Book>
      <BookName>ExampleBook1</BookName>
      <Author>Author</Author>
    </Book>
   <Book>
      <BookName>ExampleBook2</BookName>
      <Author>Author2</Author>
    </Book>
    </Books>

Now What I need to do is I need to get the row according to the first xml file, If "Author" is selected in the first XML ,I need to retrieve and display this row;

 <Book>
          <BookName>ExampleBook1</BookName>
          <Author>Author</Author>
        </Book>

What's the best way to do this in c# ?

4

2 回答 2

2

1) You can use below mentioned method to get the complete List of bookdetails of your second xml. 2) After that Apply a foreach loop according to your selection made in 1st XML.

public List<BookDetails> GetBookDetails()
        {
            XDocument xDOC = XDocument.Load("FilePath");
            List<BookDetails> bookdet = (from xele in xDOC.Descendants("Book")
                                         select new BookDetails
                                         {
                                             BookName = (string)xele.Element("BookName"),
                                             Author = (string)xele.Element("Author")
                                         }).ToList<BookDetails>();
            return bookdet;
        }

public class BookDetails
    {
        public string BookName { get; set; }
        public string Author { get; set; }
    }
于 2013-07-24T12:31:05.993 回答
1

Simply add the below code. XDocument doc = XDocument.Load("XMLFile1.xml");

        List<Books> a = new List<Books>();

        var kitap = doc.Descendants("Author");

        foreach (var item in kitap)
        {
            a.Add(new Books {AuthorName = item.Value, BookName = ""});
        }

        XDocument doc1 = XDocument.Load("XMLFile2.xml");

        List<Books> b = new List<Books>();

        var kitauthor = doc1.Descendants("Book").Where(i => i.Element("Author").Value == a[1].AuthorName).FirstOrDefault();

And

public  class Books
{
    public string AuthorName { get; set; }

    public string BookName { get; set; }

}
于 2013-07-24T12:35:50.550 回答