Click here to Skip to main content
14,866,438 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have an xml file as follows:

HTML
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<row>
		<enum>SerialPort_NotWork</enum>
		<english>Serial Port is not working restart the Computer</english>
	</row>
	<row>
		<enum>SoftwareRun_DemoMode</enum>
		<english>SOFTWARE WILL RUN IN DEMO MODE ONLY</english>

Xml file contains many rows near about 1000+.I want to write a function which accepts 'Enum' value and returns associated 'English' value.

What I have tried:

I tried following

C#
public static string GetString(String code)
      {
          XmlDocument xml = new XmlDocument();
          xml.Load(@"D:\xmlDoc\Eng_Language.xml");
          string firstName = null;
          XmlNodeList xnList = xml.SelectNodes("/Root/Row[Enum='code']");

          foreach (XmlNode xn in xnList)
          {
              firstName = xn["English"].InnerText;
          }
          return firstName;
      }


but i get XmlNode count zero.plz suggest how to search 'English' value based on 'Enum' value
Posted
Updated 16-Feb-16 20:39pm
v5
Comments
aarif moh shaikh 17-Feb-16 1:36am
   
Check your XML file node structure .

You need to select the nodes where the enum has the code, and then back up one level and go down in the english node and select it's value.

The XPath for that would look something like this;

/root/row/enum[.=\"code"]/parent::row/english


Or in code;

C#
class Program {

    // This returns an IEnumerable<string> in case there are many <row>s with the same <enum>s.
    static IEnumerable<string> GetEnglishString(XPathNavigator navigator, string code) {
        var path = String.Format("/root/row/enum[.=\"{0}\"]/parent::row/english", code);
        return navigator.Select(path).Cast<XPathNavigator>().Select(n => n.Value);
    }

    static void Main(string[] args) {

        var document = new XmlDocument();
        document.Load(@"C:\Temp\XmlExample\Eng_Language.xml");

        var navigator = document.CreateNavigator();

        var notWorking = GetEnglishString(navigator, "SerialPort_NotWork").FirstOrDefault();
        var demoMode = GetEnglishString(navigator, "SoftwareRun_DemoMode").FirstOrDefault();

        Console.WriteLine("notWorking={0}", notWorking);
        Console.WriteLine("demoMode={0}", demoMode);
    }
}


Hope this helps,
Fredrik
   
try following code.
C#
 public static string Get_XML_Text(string enumText)
        {
            try
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                //doc.LoadXml(File.ReadAllText("your xml file Path"));
                doc.LoadXml(@"
	<row>
		<enum>SerialPort_NotWork</enum>
		<english>Serial Port is not working restart the Computer</english>
	</row>
	<row>
		<enum>SoftwareRun_DemoMode</enum>
		<english>SOFTWARE WILL RUN IN DEMO MODE ONLY</english>
</row>
");
                string firstName = null;
                XmlNodeList xnList = doc.SelectNodes("/root/row/enum");
                foreach (XmlNode item in xnList)
                {
                    if (item.InnerText.Trim().Equals(enumText, StringComparison.InvariantCultureIgnoreCase))
                    {
                        firstName = xnList.Item(0).ParentNode.SelectNodes("//english").Item(0).InnerText;
                        break;
                    }
                }
                return firstName;
            }
            catch (Exception)
            {

                throw;
            }
        }

if any issue then let me know.
   
Pure Linq to XML statement:
C#
XmlDocument xdoc = XmlDocument.Load(@"D:\xmlDoc\Eng_Language.xml");
          var nodes = xdoc.Descendants("row")
                          .Where(x=>(string)x.Element("enum").Value == "code")
                          .Select(x=>(string)x.Element("english").Value);

For further information, please see:
LINQ to XML[^]
Basic Queries (LINQ to XML)[^]
LINQ to XML Overview[^]
LINQ to XML for XPath Users[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900