简体   繁体   中英

retrieve the other data that have the same keyword in xml file in c#

i saved all my data in xml file.

<myself>
  <name>sara</name>
  <age>20</age>
  <gender>female</gender>
</myself>
<myself>
   <name>bob</name>
   <age>29</age>
   <gender>male</gender>
</myself>

and there is search box which user can search all the data based on the types and keyword. for this, i am putting two types;by name and by gender.

so once the user choose name and put the keyword is sara , the output will display everything about her.

example of output should be display

sara
20
female

for this output, i make the it to display with each own textbox. there have 3 textbox title name, age and gender.

XmlDocument xml = new XmlDocument();            
        xml.Load("C:\\Users\\HDAdmin\\Documents\\Fatty\\SliceEngine\\SliceEngine\\bin\\Debug\\saya.xml");
        XmlNodeList xnList = xml.SelectNodes("/myself");            
        foreach (XmlNode xn in xnList)
        {                
            string name = xn.InnerText;                
            nameBox.Text = nameBox.Text + " " + name;
            string age = xn.InnerText;                
            ageBox.Text = ageBox.Text + " " + age;
            string gender = xn.InnerText;                
            genderBox.Text = genderBox.Text + " " + gender;
        }

i have tried using XmlNodeList xnList = xml.SelectNodes("/myself"); but it will print out everything about sara and bob.

so im guessing i could make the xml file to be like

<name>sara
   <gender>female
       <age>20</age>
   </gender>
</name>

but i think this cannot work out. is there a way for this?

this is the way to save the data.

XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("C:\\Users\\HDAdmin\\Documents\\SliceEngine\\SliceEngine\\bin\\Debug\\saya.xml");
                XmlElement contentElement = xmlDoc.CreateElement("myself");

                XmlElement nameEl = xmlDoc.CreateElement("name");
                XmlText xmlText = xmlDoc.CreateTextNode(berjaya[1]);
                nameEl.AppendChild(xmlText);
                contentElement.AppendChild(nameEl);
                xmlDoc.DocumentElement.AppendChild(contentElement);

                XmlElement ageEl = xmlDoc.CreateElement("age");
                ageEl.InnerText = berjaya[3];
                contentElement.AppendChild(ageEl);
                xmlDoc.DocumentElement.AppendChild(contentElement);

                XmlElement genEl = xmlDoc.CreateElement("gender");
                genEl.InnerText = berjaya[39];
                contentElement.AppendChild(genEl);
                xmlDoc.DocumentElement.AppendChild(contentElement);

++++++++++++====PROBLEM SOLVED====++++++++++++++++++++

thanks for the help. this have been resolved. I am using answer given by @horgh.

so here is what i learned.

1- in my xml file, i need to have main tag.

2- using if loop to retrieve data from the keyword.

and to view the other data, its going to be like this:

string name = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "name").FirstChild.Value;
            if (name == "sara")
            {
                nameBox.Text = nameBox.Text + " " + name;
                string age = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "age").FirstChild.Value;
                ageBox.Text = ageBox.Text + " " + age;
                string gender = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "gender").FirstChild.Value;
                genderBox.Text = genderBox.Text + " " + gender; 

            }

for more information you can see below. Thanks again for all the help given.

here is example

    [Test]
    public void Test()
    {
        XElement root = XElement.Load(@"C:\Data.xml");
        XElement person = FindByName(root, "sara");
        if (person == null)
        {
            return;
        }
        Console.WriteLine("Name: {0}, Age: {1}, Gender: {2}",
                          person.Element("name").Value,
                          person.Element("age").Value,
                          person.Element("age").Value);
    }

    private static XElement FindByName(XContainer root, string name)
    {
        return root.Descendants()
            .Where(x => x.Name.LocalName == "name" && x.Value == name)
            .Select(x => x.Parent)
            .FirstOrDefault();
    }

Xml file: root element has been added

<root>
  <myself>
    <name>sara</name>
    <age>20</age>
    <gender>female</gender>
  </myself>
  <myself>
    <name>bob</name>
    <age>29</age>
    <gender>male</gender>
  </myself>
</root>

Console out put:

Name: sara, Age: 20, Gender: 20

Suggestion:

  • FindByName may be should return more then 1 element, ie List<XElement>
  • Add Person class with properties Name, Age, Gender

At first, I had an error, saying that the xml has no root element, so I added root tag:

<main>
 <myself>
  <name>sara</name>
  <age>20</age>
  <gender>female</gender>
</myself>
<myself>
  <name>bob</name>
  <age>29</age>
  <gender>male</gender>
</myself>

Than I added a check for the requested name, ie "sara":

if (name == "sara")

So, the resulting code is:

        XmlDocument xml = new XmlDocument();
        xml.Load("1.xml");
        XmlNodeList xnList = xml.SelectNodes("/main/myself");
        foreach (XmlNode xn in xnList)
        {
            string name = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "name").FirstChild.Value;
            if (name == "sara")
            {
                nameBox.Text = nameBox.Text + " " + name;
                string age = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "age").FirstChild.Value;
                ageBox.Text = ageBox.Text + " " + age;
                string gender = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "gender").FirstChild.Value;
                genderBox.Text = genderBox.Text + " " + gender;                                        
            }
        }

And the output is:

sara
20
female

How about

XmlDocument xml = new XmlDocument();            
xml.Load("YourFilePath");        
foreach(var xml in doc.Elements("rootelement").Elements("myself").Elements("name")) 
{
     //Do what you want.
}

The XPath you specified will select all elements called "myself" which correctly returns both the data for sara and bob.

Try using "//myself[name=sara]" in your SelectNodes call. It should only return the myself elements that have name with a value of sara.

PS: I would also suggest renaming your myself elements to Person or something like that. :)

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