Home > Enterprise >  .Net parse XML to return value of property
.Net parse XML to return value of property

Time:01-12

I'm always getting turned around with XML. It isn't my bag. I have a .net app that i'm trying to build to capture the needed information. Here is a sample:

<?xml version="1.0"?>
<DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts">
  <DTS:Executables>
    <DTS:Executable>
      <DTS:Variables />
      <DTS:ObjectData>
        <pipeline
          version="1">
          <components>
            <component
              usesDispositions="true"
              version="4">
              <properties>
                <property
                  dataType="System.Int32"
                  description="The number of seconds before a command times out.  A value of 0 indicates an infinite time-out."
                  name="CommandTimeout">0</property>
                <property
                  dataType="System.String"
                  description="Destination"
                  name="OpenRowset">DestinationFile</property>
              </properties>
            </component>
            </components>
        </pipeline>
      </DTS:ObjectData>
    </DTS:Executable>
  </DTS:Executables>
</DTS:Executable>

This is just a snippet of a larger file with many DTS:ObjectData and properties per.

I'm attempting to pull the value "DestinationFile" from the property where the name = OpenRowset.

CodePudding user response:

Sounds like what you're looking for is a simple xpath:

//property[@name='OpenRowset']/text()

CodePudding user response:

Look into System.XML.XMLReader. It's been a while since I've used it so I can't give you example code off the top of my head but the general idea is that the Read function can be used to iterate through the xml nodes until you find one with NodeType == XmlNodeType.Element and Name == "property", then MoveToAttribute("name") to get the element's name attribute, and check if its value is "OpenRowSet". If so, there's functions to get back to the attribute's parent element and read its data.

  • Related