简体   繁体   中英

Check if XML node value already exists in xml file using c#

Please note that I'm new to C# and I learn it right now :) I couldn't find something similar to my problem, so I came here.

I have an application in which I add customers (it's in the final stage). All customers are stored in an XML file. Every single customer gets a new customer number. In my xml file I got an XmlNode called CustNo . Now if the user add a new customer and type in a number which already exist, it should pop up a message box to say that this number already exists. I got this c# code:

XDocument xdoc = XDocument.Load(path + "\\save.xml");
var xmlNodeExist = String.Format("Buchhaltung/Customers/CustNo");
var CustNoExist = xdoc.XPathSelectElement(xmlNodeExist);

if (CustNoExist != null)
{
   MessageBox.Show("asdf");
}

And my XML file looks like this:

<Buchhaltung>
    <Customers>
        <CustNo>12</CustNo>
        <Surname>Random</Surname>
        <Forename>Name</Forename>
        <Addr>Address</Addr>
        <Zip>12345</Zip>
        <Place>New York</Place>
        <Phone>1234567890</Phone>
        <Mail>example@test.com</Mail>
    </Customers>
    <Customers>
        <CustNo>13</CustNo>
        <Surname>Other</Surname>
        <Forename>Forename</Forename>
        <Addr>My Address</Addr>
        <Zip>67890</Zip>
        <Place>Manhattan</Place>
        <Phone>0987654321</Phone>
        <Mail>test@example.com</Mail>
    </Customers>
</Buchhaltung>

But then the message box always pops up. What am I doing wrong?

That's because your XPath return all CustNo elements, no matter of it's content.

Try following:

var myNumber = 12;
var xmlNodeExist = String.Format("Buchhaltung/Customers/CustNo[. = {0}]", myNumber.ToString());

or using First and LINQ to XML:

var myNumber = 12;
var xmlNodeExist = "Buchhaltung/Customers/CustNo";
var CustNoExist = xdoc.XPathSelectElements(xmlNodeExist).FirstOrDefault(x => (int)x == myNumber);

You are currently testing for existance of any 'CustNo' element. See this reference about the XPath syntax.

Your XPath should say something like this:

Buchhaltung//Customers[CustNo='12']

which would say " any customers element containing a 'CustNo' element with value = '12' "

Combining that with your current code:

var custNoGivenByCustomer = "12";
var xmlNodeExistsXpath = String.Format("Buchhaltung//Customers[CustNo='{0}']", custNoGivenByCustomer );
var CustNoExist = xdoc.XPathSelectElement(xmlNodeExistsXpath);

You can use LINQ to XML

var number = textBox1.Text;
var CustNoExist = xdoc.Descendants("CustNo").Any(x => (string)x == number);
if(CustNoExist)
{
  MessageBox.Show("asdf");
}

This is because you select the CustNo elements regardless of their value. This will filter it to the desired customer number:

int custNo = 12;
var xmlNodeExist = String.Format("Buchhaltung/Customers[CustNo={0}]", custNo);

It selects the Customers elements instead, but since you're just checking for existence, that's unimportant.

W3Schools has a good tutorial/reference on XPath .

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