none
I have this XML file and i want to take the values of <type> and <value> and print them in c# console application RRS feed

  • Question

  • <?xml version="1.0" encoding="utf-8"?>
    <properties name="root">
      <properties name="etc">
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>fileValidation</key>
          <value>-1</value>
          <readOnly>0</readOnly>
        </property>
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>logLevel</key>
          <value>0</value>
          <readOnly>0</readOnly>
        </property>
      </properties>
      <properties name="playlist">
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>type</key>
          <value>3</value>
          <readOnly>0</readOnly>
        </property>
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>fillersPath</key>
          <value></value>
          <readOnly>0</readOnly>
        </property>
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>loop</key>
          <value>True</value>
          <readOnly>0</readOnly>
        </property>
      </properties>
      <properties name="content">
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>rundownPath</key>
          <value>C%3A%5CProgramData%5CParallel%5CPlayout%5Crundown.evp</value>
          <readOnly>0</readOnly>
        </property>
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>type</key>
          <value>2</value>
          <readOnly>0</readOnly>
        </property>
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>autoSave</key>
          <value>True</value>
          <readOnly>0</readOnly>
        </property>
      </properties>
      <properties name="categories">
        <properties name="libraries" />
        <properties name="stompserver">
          <property>
            <type>0</type>
            <dataType>0</dataType>
            <key>port</key>
            <value>8888</value>
            <readOnly>0</readOnly>
          </property>
        </properties>
      </properties>
      <properties name="render">
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>displayMode</key>
          <value>HD1080+30p</value>
          <readOnly>0</readOnly>
        </property>
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>normalization</key>
          <value>0</value>
          <readOnly>0</readOnly>
        </property>
      </properties>
      <properties name="devices" />
      <properties name="engine">
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>decodingMethod</key>
          <value>True</value>
          <readOnly>0</readOnly>
        </property>
        <property>
          <type>0</type>
          <dataType>0</dataType>
          <key>timeBeforePrepareNext</key>
          <value>5000</value>
          <readOnly>0</readOnly>
        </property>
      </properties>
    </properties>
    Monday, July 15, 2019 8:25 AM

All replies

  • Simply load the document and get a list of all property-nodes:

    var doc = new XmlDocument();
    doc.Load("XMLFile.xml");
    XmlNodeList propertyList = doc.GetElementsByTagName("property");the you
    

    then you can iterate through this list:

                foreach (var item in propertyList)
                {
                    var element = (XmlNode) item;
                    var type = element.SelectSingleNode("type");
                    var value = element.SelectSingleNode("value");
    
                    Debug.Print($"type: {type.InnerText}, value: {value.InnerText}");
                }
    
    although this works for the provided XML some validation and errorhandling is highly recommended here

    Monday, July 15, 2019 10:13 AM
  • Hi Skavy,

    Thank you for posting here.

    According to your question, I make a test on my side and use XDocument to find type and value element in the xml file.

    You can refer the following code:

            static void Main(string[] args)
            {
                XDocument xmlDoc = XDocument.Load(path);
                var items = from item in xmlDoc.Descendants("property")
                            select item;
                foreach (XElement item in items)
                {
                    Console.Write($"type: {item.Element("type").Value}  ");
                    Console.WriteLine($"value: {item.Element("value").Value}");
                }
                Console.ReadKey();
            }

    Result of the test:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 16, 2019 2:41 AM
    Moderator