gpt4 book ai didi

c# - 如何在没有名称的 XML 中查找属性及其值?

转载 作者:行者123 更新时间:2023-11-30 23:03:56 25 4
gpt4 key购买 nike

我是 C# 新手

这里是 xml:

<ROOT>
<Columns BaseXPath="//Orders/Position/">
<Colum XPath="@PositionSK" Name="Position"/>
<Colum XPath="@PosGroup" Name="Gruppen-Nr"/>
<Colum XPath="@PosNumber" Name="PositionsNr"/>
<Colum XPath="@PositionCommercialTypeSK" Name="Status"/>
<Colum XPath="@BundlePositionSK" Name="BundlePositionSK"/>
<Colum XPath="@MainPositionSK" Name="MainPositionSK"/>
<Colum XPath="@SalesAgentPrice" Name="Preis"/>
<Colum XPath="@BookingUnitSK" Name="Buch"/>
<Colum XPath="@ContentComponentCommSK" Name="IKO"/>
<Colum XPath="@PositionTypeSK" Name="PositionsTyp"/>
<Colum XPath="//Advertisement[@AdvertisementSK = PositionAdvertisementRelationship/@AdvertisementSK]/@AdvertisementSK" Name="AdvertisementSK"/>
<Colum XPath="//Advertisement[@AdvertisementSK = PositionAdvertisementRelationship/@AdvertisementSK]/@AdvertisementTypeSK" Name="Formatvorgabe"/>
</Columns>
</ROOT>

这个 xml 总是可以改变的。所以它永远不一样。有时信息较多,有时较少。

这个 xml 给了我一定的信息,应该在第二个“主 xml”中搜索。

所以现在我知道我必须找到 PositionSK、PosGroup、PositionCommercialTypeSK 的属性,...。在另一个 xml 中。

但是我该怎么做呢?名称永远不会相同,所以我需要一个占位符吗?

我试过这个:

public class ResultNames
{
XmlDocument xml = new XmlDocument();

public List<ResultNames> GetRightNames (string file)
{
xml.Load(file); //this is the xml file

var list = xml.SelectNodes("//ROOT/Columns/Colum");

foreach ( XmlNode colum in list)
{
XmlNode bla = colum.Attributes; //I dont know what I can do here, without any name.
}

return null;
}

另一个 xml 文件是什么,我需要一个额外的类吗?

来自其他 xml 的小示例:

<Set>
<Orders OrderSK="0013233309" OrderTypeSK="ORDER" OrderDate="2000-01-01T12:00:00" OrderPrice="0.0000" OrderQuantity="0.00" DistrictSK="0026070180" PaymentTypeSK="E" OrderCreationTypeSK="SNW5ORD" SalesAgentSK="0020025518" ChangeDate="2018-01-25T15:48:29" SalesOrganisationSK="K10-100-1000-50-65" ChangeDateFS="2017-12-11T15:25:21" Source="CORE" Status="C">
<Position PosNumber="3" PosGroup="5" PositionTypeSK="ONL" PositionCommercialTypeSK="DEFAULT"

但它更大。

最佳答案

结合字典使用 Xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
const string FILENAME1 = @"c:\temp\test.xml";
const string FILENAME2 = @"c:\temp\test1.xml";
static void Main(string[] args)
{

XDocument doc = XDocument.Load(FILENAME1);

Dictionary<string, XElement> dict = doc.Descendants("Columns").FirstOrDefault().Elements()
.GroupBy(x => (string)x.Attribute("XPath"), y => y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());

XDocument order = XDocument.Load(FILENAME2);
List<XElement> positions = order.Descendants("Position").ToList();

foreach (XElement position in positions)
{
foreach (XAttribute attribute in position.Attributes())
{
string name = attribute.Name.LocalName;
string value = (string)attribute;

XElement element = dict["@" + name];
element.SetValue(value);


}
}



}
}

}

下面的代码只是从第一个 Xml 文件中获取名称

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
const string FILENAME1 = @"c:\temp\test.xml";
const string FILENAME2 = @"c:\temp\test1.xml";
static void Main(string[] args)
{

XDocument doc = XDocument.Load(FILENAME1);

Dictionary<string, string> dict = doc.Descendants("Columns").FirstOrDefault().Elements()
.GroupBy(x => (string)x.Attribute("XPath"), y => (string)y.Attribute("Name"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());

XDocument order = XDocument.Load(FILENAME2);
List<XElement> positions = order.Descendants("Position").ToList();

foreach (XElement position in positions)
{
foreach (XAttribute attribute in position.Attributes())
{
string name = attribute.Name.LocalName;
string value = (string)attribute;

if(dict.ContainsKey("@" + name))
{
string xName = dict["@" + name];
Console.WriteLine("Key = '{0}', Name = '{1}'", name, xName);
}
else
{
Console.WriteLine("Not in dictionary : Key = '{0}'", name);
}
}
}



}
}

}

关于c# - 如何在没有名称的 XML 中查找属性及其值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49627870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com