简体   繁体   中英

Comparing and matching XML nodes in C#

I am trying to parse this XML document and match the guid with the link nodes. I have a GUI built in C# that will allow the user to input the guid, and I am trying to spit out the corresponding link node that corresponds with it.

For example. A user enters ID: 8385522 and the program would spit out:

http://once.www.example.com

The XML is as follows:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>
    </title>
    <link>
    </link>
    <description>
    </description>
    <language>
    </language>
    <lastBuildDate>
    </lastBuildDate>
    <item>
      <title>Parsing Example</title>
      <link>http://once.www.example.com</link>
      <pubDate>Sun, 16 Sep 2012 02:44:02 </pubDate>
      <guid>8385522</guid>
    </item>
    <item>
      <title>Parsing Example 2</title>
      <link>http://once.once.www.example2.com</link>
      <pubDate>Sat, 29 Sep 2012 18:29:13 </pubDate>
      <guid>8439191</guid>
    </item>
  </channel>
</rss>

I don't have any code written for the text box that the ID is being entered in. All I have in that field is:

void TextBox1TextChanged(object sender, EventArgs e)
{

}

Do I need put the function inside the text box field? Any help is appreciated.

Edit: Here is what I have so far:

 private void button2_Click_1(object sender, EventArgs e)
        {
            Clipboard.Clear();


            if (Directory.Exists(@"c:\text"))    
            { 


            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(@"c:\text\text.xml");

            XmlDocument lDoc = new XmlDocument();
            lDoc.Load(@"c:\text\text.xml");



            XmlNodeList ctextbox = xDoc.GetElementsByTagName("guid");
            XmlNodeList link = lDoc.GetElementsByTagName("link");

I'm not sure exactly where the parsing function needs to be.

var links = from item in xdoc.Descendants("item")
            where (int)item.Element("guid") == yourGuid
            select (string)item.Element("link");

But comprehension syntax does not have keyword for selecting single value, thus you need to do links.SingleOrDefault(); to get your link.

Or you can do search with fluent API:

XDocument xdoc = XDocument.Load(@"c:\text\text.xml");
int guid = 8385522; // parse your guid from textbox

string link = xdoc.Descendants("item")
                  .Where(item => (int)item.Element("guid") == guid)
                  .Select(item => (string)item.Element("link"))
                  .SingleOrDefault();

If it is possible for searching some guid, which is not in file (looks like your case, because guid comes from textbox), then:

XDocument xdoc = XDocument.Load(@"c:\text\text.xml");
int guid = 8385525; // parse your guid from textbox

var links = from item in xdoc.Descendants("item")
            where (int)item.Element("guid") == guid
            select (string)item.Element("link");

string link = links.SingleOrDefault();
string link = GetLink(@"c:\text\text.xml", "8385522");

--

string GetLink(string xmlFile,string guid)
{
    var xDoc = XDocument.Load(xmlFile);
    var item = xDoc.Descendants("item")
                    .FirstOrDefault(x => (string)x.Element("guid") == guid);

    if (item == null) return null;
    return (string)item.Element("link");
}

I'm using this xml library , but you can use the XPath provided with .Net by including System.Linq.XPath I believe. (I can't check at the moment if that is 100% accurate).

XElement root = XElement.Load(file);
XElement guid = root.XPathElement("//guid[.={0}]", id);
XElement link = null;
if(null != guid)
    link = guid.Parent.Element("link");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM