如何检查xml文件中是否可用的ID [英] how to check the id available or not in xml file

查看:105
本文介绍了如何检查xml文件中是否可用的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这段代码不是我的 r ,可以帮我看看这段代码需要添加一些东西吗?因为我的问题是如何检查xml文件中可用的ID是否为


  protected void Button3_Click(object sender,EventArgs e)

    {

        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

        doc.Load(Server.MapPath(" Inventory.xml"));;


       

        if(TextBox1.Text.Equals(""))

        {

            Label1.Visible = true;

            Label1.Text =" Please write Id"; $
           返回;

        }
       否则为
        {

        }
        if(!System.IO.File.Exists(Server.MapPath(TextBox1.Text +" Inventory.xml"))))b
        {

            Label1.Visible = true;

            Label1.Text =" ID available";

           返回;

        }
       否则为
        {

            Label1.Visible = true;

            Label1.Text ="使用的ID,请重写" ;;

            TextBox1.Text ="" ;;

           返回;
$
            

        }
    }


我的xml文件


< products>

  < cloths id =" A01">

    < clothName> Polo Shirt< / clothName>

    <尺寸> S< /尺寸>

    <价格> 50.99< / Price>

    <金额> 100000< /金额>

    < images>图片/ shirt1.png< / images>

  < / cloths>

  < cloths id =" A02">

    < clothName> Polo Shirt< / clothName>

    <尺寸> M< /尺寸>

    <价格> 50.99< / Price>

    <金额> 100000< /金额>

    < images>图片/ shirt1.png< / images>

  < / cloths>

  < cloths id =" A03">

    < clothName> Polo Shirt< / clothName>

    <尺寸> XL< /尺寸>

    <价格> 50.99< / Price>

    <金额> 100000< /金额>

    < images>图片/ shirt1.png< / images>

  < / cloths>


< / products>

解决方案

如果你想检查是否有"布料",具有特定"id"的元素属性值然后使用例如。


  if(doc.SelectSingleNode(string.Format(" products / cloths [@id ='{0}']",TextBox1.Text))!= null)


  {


     //现在处理具有给定id的元素存在的情况


  }


 否则{


    //在此处理具有给定ID的元素不存在的情况


  }


如果要搜索具有特定id属性值的任何类型的元素,请将XPath参数更改为SelecttSingleNode,例如



if(doc.SelectSingleNode(string.Format(" // * [@ id ='{0}']",TextBox1.Text))!= null)



this code is not working, can help me see this code need to add somethings? because my problem is how to check the id available or not in xml file

 protected void Button3_Click(object sender, EventArgs e)
    {
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.Load(Server.MapPath("Inventory.xml"));

       
        if (TextBox1.Text.Equals(""))
        {
            Label1.Visible = true;
            Label1.Text = "Please write Id";
            return;
        }
        else
        {
        }
        if (!System.IO.File.Exists(Server.MapPath(TextBox1.Text + "Inventory.xml")))
        {
            Label1.Visible = true;
            Label1.Text = "ID available";
            return;
        }
        else
        {
            Label1.Visible = true;
            Label1.Text = "ID used, please rewrite";
            TextBox1.Text = "";
            return;
            
        }
    }

my xml file

<products>
  <cloths id="A01">
    <clothName>Polo Shirt</clothName>
    <Size>S</Size>
    <Price>50.99</Price>
    <Amount>100000</Amount>
    <images>Image/shirt1.png</images>
  </cloths>
  <cloths id="A02">
    <clothName>Polo Shirt</clothName>
    <Size>M</Size>
    <Price>50.99</Price>
    <Amount>100000</Amount>
    <images>Image/shirt1.png</images>
  </cloths>
  <cloths id="A03">
    <clothName>Polo Shirt</clothName>
    <Size>XL</Size>
    <Price>50.99</Price>
    <Amount>100000</Amount>
    <images>Image/shirt1.png</images>
  </cloths>

</products>

解决方案

If you want to check whether there is a "cloths" element with a certain "id" attribute value then use e.g.

  if (doc.SelectSingleNode(string.Format("products/cloths[@id = '{0}']", TextBox1.Text)) != null)

  {

     // now handle case here that element with given id exists

  }

  else {

    // handle case here that element with given id does not exist

  }

If you want to do a search for any kind of element with a certain id attribute value then change the XPath argument to SelecttSingleNode to e.g.

if (doc.SelectSingleNode(string.Format("//*[@id = '{0}']", TextBox1.Text)) != null)


这篇关于如何检查xml文件中是否可用的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆