ChoETL
ChoETL copied to clipboard
ChoETLXmlReader does not load Include attribute of PackageReference in ItemGroup in NET Core project file
I am trying to read a simple NET Core project file that has several PackageReference blocks in it. I copied the syntax of the blocks from a project file that was generated by Visual Studio. As you can see from the example below, VStudio puts the name and version of the packages in attributes in the PackageReference XmlElement.
But my code (based on your nice method of LoadXml(xmlText) does not seem to load the Include attributes, so I cannot parse the project file to determine what packages it depends on. Following your nice practice of creating Fiddles to share code issues and solutions, I created a new fiddle to illustrate the issue. The fiddle is NET 6 and ChoETL 1.2.1.47, but it still shows the same problem I am having with version 1.2.1.28.
These two fiddles only differ with the leading "<?xml version...." line. Everything else is the same. Both show the same problem. My code decorates xml attributes with [XmlAttribute], but some [XmlElements] are not decorated (you said in another issue that XmlElements did not require decorating). In general, the ChoETLXmlReader works very well on my 70+ projects. But on this project, I am trying to find out the package names that the projects consume, and the XmlReader seems not to load the PackageReferenceClass fields.
Thank you for an excellent library. I'm one of the millions of people who appreciate your work.
https://dotnetfiddle.net/LVSm6K https://dotnetfiddle.net/ahpTYv
static string coreXml = @" <Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<Platforms>AnyCPU</Platforms>
<PlatformTarget>AnyCPU</PlatformTarget>
<TargetFramework>net5.0-windows7.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include=""Microsoft.NET.Test.Sdk"" Version=""17.0.0"" />
<PackageReference Include=""MSTest.TestAdapter"" Version=""2.2.8"" />
<PackageReference Include=""MSTest.TestFramework"" Version=""2.2.8"" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include=""..\HsLogger\HsLogger.csproj"" />
</ItemGroup>
</Project>";
I found the problem. An [XmlElement] attribute is required for elements that are lists.
public class ItemGroupClass { [XmlAttribute] public string Label { get; set; } = string.Empty; // ... [XmlElement] // this is required even if elements are not supposed to require [XmlElement] public List<PackageClass> PackageReference { get; set; } }
Maybe there are unspoken rules for decorating XmlElements, such as (I'm guessing):
- not required for elements at the top level
- but required if the field is a List
? - and required if the field is at the second level (in objects (PackageRefClass) contained in objects at the top level (ItemGroup)?
Could you please recommend a best practice for using [XmlElement] and [XmlAttribute] decorations for VS project files that involve several layers of nested XmlElements that can form lists? Thank you