Home > Enterprise >  How can I select an xml node by Name and Number using C#
How can I select an xml node by Name and Number using C#

Time:10-17

I have the following xml structure:

<TacRef>        
    <TacRefData Num="0">    
        <MainData>
            <Name>Name 1</Name>
            <PicName>PicName 1</PicName>
        </MainData>
        <CategoryData Num="0">
            <CategoryTitle>General Information</CategoryTitle>
            <CategoryDescription>CategoryDescription 0</CategoryDescription>
        </CategoryData>
        <CategoryData Num="1">
            <CategoryTitle>Dimensions</CategoryTitle>
            <CategoryDescription>CategoryDescription 1</CategoryDescription>
        </CategoryData>
        <RwrData>
            <Index>0</Index>
            <Image>0</Image>
            <SearchTone>0</SearchTone>
            <LockTone>0</LockTone>
            <Name>No Radar</Name>
        </RwrData>
    </TacRefData>

    <TacRefData Num="1">    
        <MainData>
            <Name>Name 1</Name>
            <PicName>PicName 1</PicName>
        </MainData>
        <CategoryData Num="0">
            <CategoryTitle>General Information</CategoryTitle>
            <CategoryDescription>CategoryDescription 0</CategoryDescription>
        </CategoryData>
        <CategoryData Num="1">
            <CategoryTitle>Dimensions</CategoryTitle>
            <CategoryDescription>CategoryDescription 1</CategoryDescription>
        </CategoryData>
        <RwrData>
            <Index>0</Index>
            <Image>0</Image>
            <SearchTone>0</SearchTone>
            <LockTone>0</LockTone>
            <Name>No Radar</Name>
        </RwrData>
    </TacRefData>
</TacRef>   

I am trying to get each node Name and CategoryData/CategoryDescription only for Name containign "SA-" and CategoryData Num="4" (I did not show Num="4" in the example above)

I can find the Name containing "SA-" but I do not know how to get only the CategoryData with Num="4".

I am doing this:

  public class RefReader
    {
        private string _refFile = @"c:\....\TacRefDB.xml";
        public XmlDocument xDoc;

        public RefReader()
        {
            xDoc = new XmlDocument();
            xDoc.Load(_refFile);
            var nodes = xDoc.SelectNodes("TacRef/TacRefData");
            foreach (XmlNode productNode in nodes)
            {
                var name = productNode["MainData"]["Name"].InnerXml;
                if (name.Contains("SA-"))
                {
                    Console.WriteLine(name);
                    //here I want to get productNode["CategoryData"]["CategoryDescription"] with Num="4" => how do I do that?
                }
            }
        }
    }
}

CodePudding user response:

I believe you can get the Name and the CategoryData/CategoryDescription in one XPATH Query.

So modify the query below as you like, and try it :

//TacRefData[starts-with(./MainData/Name,'EC')]/(./MainData/Name|./CategoryData[@Num='0']/CategoryDescription)

The result would be :

enter image description here

  • Related