I am trying to parse an xml string and I want just the PackageReference Include attribute details and the Version of it. When I say ET.fromstring(xml) it gives error like xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 284, column 84
. I verified if the xml is valid on multiple websites and there seems to be no issues with my xml and it is well formed. Please see my Edit below.
import xml.etree.ElementTree as ET
thisDict={}
def parseXMLInCsProj(xml):
tree = ET.fromstring(xml)
for packageReference in tree.findall('.//PackageReference'):
if packageReference is None:
continue
if 'Include' in packageReference:
key=packageReference.get('Include')
child_item=packageReference.find('.//Version').text
thisDict[key]=child_item
xmlstr=f'''<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="5.1.1" targetFramework="net481" />
<package id="Moq" version="4.18.4" targetFramework="net481" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net481" />
<package id="xunit.runner.visualstudio" version="2.4.5" targetFramework="net481" developmentDependency="true" />
</packages>'''
xmlstr1=f'''<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>fdgfdgfg</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Labcyte.Echo.Server</RootNamespace>
<AssemblyName>EchoServer</AssemblyName>
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
<SccProjectName>
</SccProjectName>
<SccLocalPath>
</SccLocalPath>
<SccAuxPath>
</SccAuxPath>
<SccProvider>
</SccProvider>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup>
<StartupObject>
</StartupObject>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\\x64\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="InstallerUtil">
<HintPath>..\..\..\Library\EchoNET\EchoNETServer\InstallerUtil.dll</HintPath>
</Reference>
<Reference Include="Labcyte.Core.CommonInterface">
<HintPath>..\..\..\Library\Core\CoreClient\Labcyte.Core.CommonInterface.dll</HintPath>
</Reference>
<Reference Include="Labcyte.Core.EventServer">
<HintPath>..\..\..\Library\Core\CoreClient\Labcyte.Core.EventServer.dll</HintPath>
</Reference>
<Reference Include="Labcyte.Core.Interface">
<HintPath>..\..\..\Library\Core\CoreClient\Labcyte.Core.Interface.dll</HintPath>
</Reference>
<Reference Include="Labcyte.EchoNET.Common, Version=0.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Library\EchoNET\EchoNETServer\Labcyte.EchoNET.Common.dll</HintPath>
</Reference>
<Reference Include="Labcyte.EchoNET.Data, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Library\EchoNET\EchoNETServer\Labcyte.EchoNET.Data.dll</HintPath>
</Reference>
<Reference Include="Labcyte.EchoNET.EchoNETScripts, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Library\EchoNET\EchoNETServer\Labcyte.EchoNET.EchoNETScripts.dll</HintPath>
</Reference>
<Reference Include="Labcyte.EchoNET.HostServices">
<HintPath>..\..\..\Library\EchoNET\EchoNETServer\Labcyte.EchoNET.HostServices.dll</HintPath>
</Reference>
<Reference Include="Labcyte.EchoNET.Instrument, Version=0.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Library\EchoNET\EchoNETServer\Labcyte.EchoNET.Instrument.dll</HintPath>
</Reference>
<Reference Include="Labcyte.EchoNET.Interface, Version=0.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Library\EchoNET\EchoNETServer\Labcyte.EchoNET.Interface.dll</HintPath>
</Reference>
<Reference Include="Labcyte.EchoNET.ServiceEndpoints, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Library\EchoNET\EchoNETServer\Labcyte.EchoNET.ServiceEndpoints.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="EchoCallBack_HostService.cs" />
<Compile Include="Echo_HostService.cs" />
<Compile Include="Host.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="ProjectInstaller.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ProjectInstaller.designer.cs">
<DependentUpon>ProjectInstaller.cs</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="app.config">
<SubType>Designer</SubType>
</None>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
<Visible>False</Visible>
<ProductName>Windows Installer 3.1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Pico\Medman\AppsWrapper\AppsWrapper.vcxproj">
<Project>hghjgj</Project>
<Name>AppsWrapper</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Pico\Medman\FluidTransferInterface\FluidTransferInterface.csproj">
<Project>434554</Project>
<Name>FluidTransferInterface</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Pico\Medman\HarrierFluidTransfer\HarrierFluidTransfer.csproj">
<Project>7686877</Project>
<Name>HarrierFluidTransfer</Name>
</ProjectReference>
<ProjectReference Include="..\..\EchoServerAPI\EchoServerAPI.csproj">
<Project>89798798879</Project>
<Name>EchoServerAPI</Name>
</ProjectReference>
<ProjectReference Include="..\..\Labcyte.Echo.EchoScripts\Labcyte.Echo.EchoScripts.csproj">
<Project>7687687878787</Project>
<Name>Labcyte.Echo.EchoScripts</Name>
</ProjectReference>
<ProjectReference Include="..\EchoServerAPI.Server\EchoServerAPI.Server.csproj">
<Project>76567687887</Project>
<Name>EchoServerAPI.Server</Name>
</ProjectReference>
<ProjectReference Include="..\Labcyte.Echo.Commands\Labcyte.Echo.Commands.csproj">
<Project>878989808899</Project>
<Name>Labcyte.Echo.Commands</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ML">
<Version>2.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.ML.CpuMath">
<Version>2.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.ML.DataView">
<Version>2.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.ML.OnnxRuntime">
<Version>1.14.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.ML.OnnxTransformer">
<Version>2.0.1</Version>
</PackageReference>
<PackageReference Include="Mono.pdb2mdb">
<Version>0.1.0.20130128</Version>
</PackageReference>
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation">
<Version>4.3.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="ProjectInstaller.resx">
<DependentUpon>ProjectInstaller.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>rmdir /S /Q "$(TargetDir)ScriptClasses"
mkdir "$(TargetDir)ScriptClasses"
rmdir /S /Q "$(SolutionDir)..\Library\Echo\EchoServer"
mkdir "$(SolutionDir)..\Library\Echo\EchoServer"
mkdir "$(SolutionDir)..\Library\Echo\EchoServer\ScriptClasses"
</PreBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PostBuildEvent>copy "$(TargetDir)*.dll" "$(SolutionDir)..\Library\Echo\EchoServer"\"
copy "$(TargetDir)*.pdb" "$(SolutionDir)..\Library\Echo\EchoServer"\"
copy "$(SolutionDir)..\Library\EchoNET\EchoNETServer\ScriptClasses\*.cs" "$(TargetDir)ScriptClasses\"
copy "$(TargetDir)ScriptClasses\" "$(SolutionDir)..\Library\Echo\EchoServer"\ScriptClasses\"
copy "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\\v4.8.1\Facades\System.Linq.*" "$(TargetDir)"
copy "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\\v4.8.1\System.Core.dll" "$(TargetDir)"
copy "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\\v4.8.1\Facades\\netstandard.dll" "$(TargetDir)"
</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>'''
parseXMLInCsProj(xmlstr1)
EDIT: As suggested I updated my xml string. Now I don't see the error. But as soon as the debugger comes to the line for packageReference in tree.findall('.//PackageReference'):
, the program terminates. Please help.
source https://stackoverflow.com/questions/77018449/et-fromstring-gives-parseerror
Comments
Post a Comment