Curtis J.
Curtis J.

Reputation: 63

How do I get one level down from XDocument.Root?

For example, from root (PBC) i want to return one level down (PBCVersion, ProjectName, ProjectVersion, Targets, Packages) but instead, with:

foreach (XElement x in oPbcXDocument.Root.Elements())
{
    Console.WriteLine(x.Value);
}

i'm returning:

PBC
1
dfcs
1102
RCM565bas_build.bat RCM565scm-00110binlogWINSIMbas_build.bat SIMscm-00110binlog
bas_package.batscm-00110PackageoutlogDFCS1102RCM565LastSuccessfulComponentsDFCS1102WINSIMLastSuccessfulComponents2

here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<PBC>
  <PBCVersion>1</PBCVersion>
  <ProjectName>dfcs</ProjectName>
  <ProjectVersion>1102</ProjectVersion>
  <Targets>
    <Target>
      <TargetName>RCM565</TargetName>
      <CommandLine>bas_build.bat RCM565</CommandLine>
      <BuildEnvTypeName>scm-00110</BuildEnvTypeName>
      <RelativeOutputPath>bin</RelativeOutputPath>
      <RelativeLogPath>log</RelativeLogPath>
      <Dependencies/>
    </Target>
    <Target>
      <TargetName>WINSIM</TargetName>
      <CommandLine>bas_build.bat SIM</CommandLine>
      <BuildEnvTypeName>scm-00110</BuildEnvTypeName>
      <RelativeOutputPath>bin</RelativeOutputPath>
      <RelativeLogPath>log</RelativeLogPath>
      <Dependencies/>
    </Target>
  </Targets>
  <Packages>
    <Package>
      <CommandLine>bas_package.bat</CommandLine> 
      <BuildEnvTypeName>scm-00110</BuildEnvTypeName> 
      <PackageName>Package</PackageName> 
      <RelativeOutputPath>out</RelativeOutputPath> 
      <RelativeLogPath>log</RelativeLogPath> 
      <Dependencies>
        <BuildDependency>
          <ProjectName>DFCS</ProjectName>
          <ProjectVersion>1102</ProjectVersion>
          <TargetName>RCM565</TargetName>
          <ProjectBuild>LastSuccessful</ProjectBuild> 
          <RelativeSandboxPath>Components</RelativeSandboxPath> 
        </BuildDependency>
        <BuildDependency>
          <ProjectName>DFCS</ProjectName>
          <ProjectVersion>1102</ProjectVersion>
          <TargetName>WINSIM</TargetName>
          <ProjectBuild>LastSuccessful</ProjectBuild> 
          <RelativeSandboxPath>Components2</RelativeSandboxPath> 
        </BuildDependency>
      </Dependencies>
    </Package>
  </Packages>
</PBC>

How would I be able to return one level down ONLY?

Upvotes: 1

Views: 469

Answers (2)

har07
har07

Reputation: 89305

Your code is correct to get all elements one level down the Root, but it isn't clear what kind of output you want to get. I assume you want to get complete markup of those aforementioned elements :

foreach (XElement x in oPbcXDocument.Root.Elements()) 
{ 
    Console.WriteLine(x.ToString()); 
}

Upvotes: 2

Darek
Darek

Reputation: 4797

Don't use Value property as it will always contain the text representation of all descendants. Process the elements as needed.

Upvotes: 0

Related Questions