none
Beginner XML question

    Question

  • I want to read the "setParameters.xml" file that is part of some Visual Studio builds.  The information I want is the IIS  Web Application Name.  I know for something this straightforward it should be simple, but ... I still havent taken the time to learn XML notations and terminology so I dont know what it is I am trying to do, in XML speak.

    <parameters>
      <setParameter name="IIS Web Application Name"
        value="Default Web Site/WebApplication1_deploy" />
     

    How would I read this value from XML?

    Saturday, January 04, 2014 9:10 PM

Answers

  • Hello danielsn,

    Since I do not know the exact XML file, I assume it as below and use C#:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <parameters>
    
      <setParameter name="IIS Web Application Name"
    
        value="Default Web Site/WebApplication1_deploy" />
    
    </parameters>
    

    We can use XPath to read it:

    string xmlFile = @"E:\BMX\SourceFile\2014\01\S06.xml";
    
    
                XDocument doc = XDocument.Load(xmlFile);
    
    
                string setParameterName = doc.XPathSelectElement("/parameters/setParameter").Attribute("name").Value;
    

    You just need to replace the file path and the element path with yours. And adding the using System.Xml.XPath namespace because XPathSelectElement is an extension under this namespace.

    The result is:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by danielsn Tuesday, January 07, 2014 7:00 PM
    Monday, January 06, 2014 2:04 AM
    Moderator

All replies

  • Hello danielsn,

    Since I do not know the exact XML file, I assume it as below and use C#:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <parameters>
    
      <setParameter name="IIS Web Application Name"
    
        value="Default Web Site/WebApplication1_deploy" />
    
    </parameters>
    

    We can use XPath to read it:

    string xmlFile = @"E:\BMX\SourceFile\2014\01\S06.xml";
    
    
                XDocument doc = XDocument.Load(xmlFile);
    
    
                string setParameterName = doc.XPathSelectElement("/parameters/setParameter").Attribute("name").Value;
    

    You just need to replace the file path and the element path with yours. And adding the using System.Xml.XPath namespace because XPathSelectElement is an extension under this namespace.

    The result is:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by danielsn Tuesday, January 07, 2014 7:00 PM
    Monday, January 06, 2014 2:04 AM
    Moderator
  • Thank you!
    The part I was missing was the added namespace.

    • Edited by danielsn Tuesday, January 07, 2014 7:06 PM
    Tuesday, January 07, 2014 7:00 PM