Justin Van Bibber
Justin Van Bibber

Reputation: 393

Search xml file for a given inner text value, return its parent

I have an xml document that looks like so:

<MyDataSet>
    <Q>
        <SKU> Unique Value </SKU>
        <description> non unique </description>
    </Q>

    <Q>
       ...
    </Q>

     ... any number of <Q> elements ...

</MyDataSet

I would like to search through this XML document for a given SKU, and have access to the parent Q node from there. Each SKU is unique. Is there a way to do so cleanly? I'm looking to extract an item's description from a given SKU. I should add I'm working in C#

Upvotes: 0

Views: 2289

Answers (3)

user2391759
user2391759

Reputation: 280

If you are doing more work with this document you might want to use the XmlSerializer. It is a little bit of work but will give you better performance and ease in the long run.

use a model

/// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class MyDataSet
        {

            private MyDataSetQ[] qField;

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Q")]
            public MyDataSetQ[] Q
            {
                get
                {
                    return this.qField;
                }
                set
                {
                    this.qField = value;
                }
            }
        }

        /// <remarks/>
        [System.SerializableAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class MyDataSetQ
        {

            private string sKUField;

            private string descriptionField;

            /// <remarks/>
            public string SKU
            {
                get
                {
                    return this.sKUField;
                }
                set
                {
                    this.sKUField = value;
                }
            }

            /// <remarks/>
            public string description
            {
                get
                {
                    return this.descriptionField;
                }
                set
                {
                    this.descriptionField = value;
                }
            }
        }

then this code

var serializer = new XmlSerializer(typeof(MyDataSet));
var xmlObject = (MyDataSet)serializer.Deserialize(STREAM);

STREAM can be a file stream or a web response. Its a little more work to setup but if you are going to work with xml its the best way. The class was made from an vs gallery package that had PasteAsXml types

Upvotes: 1

MRebai
MRebai

Reputation: 5474

You just need to use LINQ, hope tht sample could help you :

XDocument document = XDocument.Load(filePath);
XElement wantedNode= document .Root.Elements("Q").Where(x => x.Element("SKU").Value == " Unique Value ").FirstOrDefault();

Upvotes: 1

palehorse
palehorse

Reputation: 27476

If your XML contained in an XDocument or something like that you could simply use LINQ to do what you're looking for. Something line this would get that first element.

XDocument doc = XDocument.Load(filename); // Path to the XML file from the post.
XElement node = doc.Root.Elements("Q").Where(e => e.Element("SKU").Value == " Unique Value ").FirstOrDefault();

Upvotes: 3

Related Questions