[英]Parsing XML, getting deep elements of specific name. (c#)
I would like to ask you about parsing of XML, I try it by many ways but never get result what I "want/expect".我想问你关于 XML 的解析,我尝试了很多方法,但从来没有得到我“想要/期望”的结果。 I would like to get ALL elements of specific name ("packageReference") and his attributes/children(elements).我想获取特定名称的所有元素(“packageReference”)和他的属性/子项(元素)。 After that I will make collection and parse the result to get value of "include" attribute and value of element/atribute with name "version"之后,我将收集并解析结果以获取“包含”属性的值和名称为“版本”的元素/属性的值
example of xml (my elements which I am interest is on bottom): xml 的示例(我感兴趣的元素在底部):
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="Launch.cs" />
<Compile Include="Launch.designer.cs">
<DependentUpon>Launch.cs</DependentUpon>
</Compile>
<Compile Include="Main.cs" />
<None Include="Info.plist">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Model\Project.Model.csproj">
<Project>{0511395F-513C-4F56-BF87-718CA49BB13B}</Project>
<Name>Project.Model</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper">
<Version>7.0.1</Version>
</PackageReference>
<PackageReference Include="GMImagePicker.Xamarin">
<Version>2.5.0</Version>
</PackageReference>
<PackageReference Include="IdentityModel">
<Version>4.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.AppCenter">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.AppCenter.Analytics">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.AppCenter.Crashes">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="Package.Test" Version="1.1.21" />
</ItemGroup>
<ItemGroup />
</Project>
The result should be:结果应该是:
AutoMapper;7.0.1
GMImagePicker.Xamarin;2.5.0
...
...
Package.Test;1.1.21
My code:我的代码:
var text = System.IO.File.ReadAllText(@"C:\MyGit\TestXML3.csproj");
var root = XElement.Parse(text);
var packageReferencesElements = root.Descendants("PackageReference");
var packageReferencesElementsAndSelf = root.DescendantsAndSelf("PackageReference");
var packageReferencesElementsOffElement = root.Elements().DescendantsAndSelf("PackageReference");
I tried many ways and few of them you can see in code.我尝试了很多方法,但很少有人可以在代码中看到。 I dont want to "go" into every element which have child and checking in loop if there is something what I can be interest.如果有我感兴趣的东西,我不想“进入”每个有子元素并在循环中检查的元素。 There must be better way.一定有更好的办法。 And point there is that I am not sure if my elements which I am looking for is in 2 layer or even deeper.还有一点,我不确定我正在寻找的元素是否在 2 层或更深。
Update : this code is working to get my information, when I know how deep they are... But when I dont know how deep it should be?更新:当我知道它们有多深时,这段代码正在努力获取我的信息......但是当我不知道它应该有多深时?
var root = XElement.Parse(fileString);
var itemGroupsELements = root.Elements().Where(x => x.Name.LocalName == "ItemGroup");
var packageReferenceElements = itemGroupsELements.Elements().Where(x => x.Name.LocalName == "PackageReference");
foreach (var packageReference in packageReferenceElements)
{
string packageName = packageReference.Attribute("Include").Value;
string version = (packageReference.Attribute("Version") != null)
? packageReference.Attribute("Version").Value
: packageReference.Value;
_packageList.Add(new Package(){App = app, PackageName = packageName, PackageVersion = version, Project = projectName});
}
Any idea what I am doing wrong?知道我做错了什么吗? Thank you!谢谢!
PackageReference
(like all the other elements in the file) is in the XML namespace "http://schemas.microsoft.com/developer/msbuild/2003". PackageReference
(与文件中的所有其他元素一样)位于 XML 命名空间“http://schemas.microsoft.com/developer/msbuild/2003”中。 You have to specify the namespace in your call to Descendants
:您必须在对Descendants
的调用中指定命名空间:
XNamespace build = "http://schemas.microsoft.com/developer/msbuild/2003";
var packageReferencesElements = root.Descendants(build + "PackageReference");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.