-1

When i try M Adeel Khalid kode i get nothing, and trying others i get errors. i miss something, but i cant se it. My code look like this. but i get an error on Descendants, Saying "xmlDocument does not contain a definition for descendants" As you can probably see, I'm pretty new to this, so bear with me.

 protected void btnRetVare_Click(object sender, EventArgs e)
{
    fldRetVare.Visible = true;
    try
    {
        functions func = new functions();
       bool exists = func.checForMatch(txtRetVare.Text);
        string myNumber = txtRetVare.Text;

        if (Page.IsValid)
        {
            if (!exists)
            {
                txtRetVare.Text= "Varenummer findes ikke";
            }
            else
            {
                XmlDocument xmldoc = new XmlDocument();
                //xmldoc.Load(Server.MapPath(map));
                xmldoc.LoadXml(Server.MapPath(map));

                //var Varenummer2055component = xmldoc.SelectNodes("s/Reservedele/Component[Varenummer/text()='"+txtRetVare+"']/Remarks");

                //if (Varenummer2055component.Count == 1)
                //{
                //    var remarks = Varenummer2055component[0].InnerText;
                //    txtRetBemærkninger.Text = remarks.ToString();
                //}

                string remarks = (from xml2 in xmldoc.Descendants("Component")
                                  where xml2.Element("Varenummer").Value == txtRetVare.Text
                                  select xml2.Element("Remarks")).FirstOrDefault().Value;

                txtRetBemærkninger.Text = remarks;



            }

        }
TheNewone
  • 97
  • 1
  • 10

3 Answers3

1

You can get it this way.

    XDocument xdoc = XDocument.Load(XmlPath);
    string remarks = (from xml2 in xdoc.Descendants("Component")
                                    where xml2.Element("Varenummer").Value == "2055"
                                    select xml2.Element("Remarks")).FirstOrDefault().Value;

I've tested this code. Hope it helps.

M. Adeel Khalid
  • 1,786
  • 2
  • 21
  • 24
  • See the edited answer. It will get you the inner text of Remarks for the first node. If you need anything else from `LINQ`, drop a comment. – M. Adeel Khalid Feb 13 '17 at 05:48
1

Use XPath to select the correct node:

XmlDocument xml = new XmlDocument();
xml.LoadXml(@"
<Reservedele>
      <Component>
        <Type>Elektronik</Type>
        <Art>Wheel</Art>
        <Remarks>erter</Remarks>
        <Varenummer>2055</Varenummer>
        <OprettetAf>jg</OprettetAf>
        <Date>26. januar 2017</Date>
      </Component>
      <Component>
        <Type>Forbrugsvarer</Type>
        <Art>Bulb</Art>
        <Remarks>dfdh</Remarks>
        <Varenummer>2055074</Varenummer>
        <OprettetAf>jg</OprettetAf>
        <Date>27. januar 2017</Date>
      </Component>
</Reservedele>");

var Varenummer2055component = xml.SelectNodes("s/Reservedele/Component[Varenummer/text()='2055']/Remarks");

if (Varenummer2055component.Count == 1)
{
    var remarks = Varenummer2055component[0].InnerText;
}
Serge
  • 3,986
  • 2
  • 17
  • 37
0

I think extension method First of LINQ to XML will be simple enough and fill requirements of your questions.

var document = XDocument.Load(pathTopXmlFile);
var remark = 
    document.Descendants("Component")
            .First(component => component.Element("Varenummer").Value.Equals("2055"))
            .Element("Remarks")
            .Value;

First method will throw exception if xml doesn't contain element with Varenummer = 2055

In case where there is possibility that given number doesn't exists in the xml file you can use FirstOrDefault extension method and add checking for null

var document = XDocument.Load(pathTopXmlFile);
var component = 
    document.Descendants("Component")
            .FirstOrDefault(comp => comp.Element("Varenummer").Value.Equals("2055"));

var remark = component != null ? component.Element("Remarks").Value : null;

For saving new value you can use same "approach" and after setting new value save it to the same file

var document = XDocument.Load(pathTopXmlFile);
var component = 
    document.Descendants("Component")
            .FirstOrDefault(comp => comp.Element("Varenummer").Value.Equals("2055"));

component.Element("Remarks").Value = newValueFromTextBox;

document.Save(pathTopXmlFile);

One more approach, which will be overkill in your particular case, but can be useful if you use other values of xml. This approach is serialization.

You can create class which represent data of your xml file and then just use serialization for loading and saving data to the file. Examples of XML Serialization

Fabio
  • 31,528
  • 4
  • 33
  • 72