我试图解析具有重复结构块的XML文件,但是结构块中的元素值不同。我需要存储重复结构块中的所有值。我无法找出一种有效的方法。
我能够存储Masterdata部分。另外,我试图将别名块存储在列表中的不同块中,但是它很凌乱。我无法确定该值来自哪一部分。
public class PRecord : ConnectorDoc
{
public string creationDate { get; internal set; }
public string system { get; internal set; }
public string partReference { get; internal set; }
public string partNumber { get; internal set; }
public string partNumber_confidence { get; internal set; }
public string partType { get; internal set; }
public string description { get; internal set; }
public string revision { get; internal set; }
public string status { get; internal set; }
public string part_type { get; internal set; }
public string part_source { get; internal set; }
public string category { get; internal set; }
public string source { get; internal set; }
public string productLifeCycle { get; internal set; }
public List<ARecord> aliases { get; internal set; }
}
public class ARecord : ConnectorDoc
{
public string a_partNumber { get; set; }
public string a_ManufName { get; set; }
public string a_DnBCode { get; set; }
public string a_category { get; set; }
}
private IEnumerable<PRecord> parse_file(string xml_file)
{
XNamespace ns = "";
XDocument xmlDoc = XDocument.Parse(xml_file);
IEnumerable<PRecord> records = from pr in xmlDoc.Descendants("result")
select new PRecord
{
Id = Sys.NewId(),
Version = Sys.GetVersionDate(),
SourceStr1 = pr.Descendants("MasterData").ElementAt(0).Element("MasterSystem") == null ? "" : pr.Descendants("MasterData").ElementAt(0).Element("MasterSystem").Value,
SourceStr2 = pr.Descendants("MasterData").ElementAt(0).Element("MasterPartType") == null ? "" : pr.Descendants("MasterData").ElementAt(0).Element("MasterPartType").Value,
SourceStr3 = pr.Descendants("MasterData").ElementAt(0).Element("PartNumber") == null ? "" : pr.Descendants("MasterData").ElementAt(0).Element("PartNumber").Value,
SourceStr4 = pr.Descendants("MasterData").ElementAt(0).Element("Revision") == null ? "" : pr.Descendants("MasterData").ElementAt(0).Element("Revision").Value,
SourceStr5 = pr.Descendants("MasterData").ElementAt(0).Element("Description") == null ? "" : pr.Descendants("MasterData").ElementAt(0).Element("Description").Value,
SourceStr6 = pr.Descendants("MasterData").ElementAt(0).Element("UoM") == null ? "" : pr.Descendants("MasterData").ElementAt(0).Element("UNSPSC").Value,
source = pr.Element("source") == null ? "" : pr.Element("source").Value,
category = pr.Element("category") == null ? "" : pr.Element("category").Value,
productLifeCycle = pr.Element("productLifeCycle") == null ? "" : pr.Element("productLifeCycle").Value,
aliases = (from alias in pr.Descendants("alias")
select new ARecord
{
a_partNumber = alias.Element("partNumber") == null ? "" : alias.Element("partNumber").Value,
a_ManufName = alias.Element("ManufName") == null ? "" : alias.Element("ManufName").Value,
a_DnBCode = alias.Element("DnBCode") == null ? "" : alias.Element("DnBCode").Value,
a_category = alias.Element("category") == null ? "" : alias.Element("category").Value,
}).ToList(),
};
return records;
}
示例XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<MasterData>
<MasterSystem>ABC</MasterSystem>
<MasterPartType>SP</MasterPartType>
<MasterCode />
<PartNumber>CV123</PartNumber>
<Revision>A</Revision>
<Description>Test1</Description>
<UoM>EA</UoM>
<UNSPSC>3212</UNSPSC>
</MasterData>
<AliasSystem>
<envelope xmlns="">
<headers>
<creationDate xmlns="">2019-04-08</creationDate>
</headers>
<triples />
<instance>
<partReference xmlns="">
<system>Sys1</system>
<partNumber confidence="100">DTR342</partNumber>
<partType>SP</partType>
<description>Test2</description>
<status>Current</status>
<revision>-</revision>
<UNSPSC />
<UoM>EA</UoM>
<type>A_SP</type>
<source>S1</source>
<category>pref</category>
<productLifeCycle>False</productLifeCycle>
<aliasList>
<alias valid="true" confidence="100" reference="MR">
<partNumber>R413I2100#00M</partNumber>
<ManufName>KM</ManufName>
<DnBCode>78842</DnBCode>
<category>MR</category>
</alias>
<alias valid="true" confidence="100" reference="Manu">
<partNumber>PME271</partNumber>
<ManufName>KE</ManufName>
<DnBCode>788421</DnBCode>
<category>MR</category>
</alias>
</aliasList>
</partReference>
</instance>
<attachments />
</envelope>
</AliasSystem>
<MasterSystem>
<envelope xmlns="">
<headers>
<creationDate xmlns="">2019-04-10</creationDate>
</headers>
<triples />
<instance>
<partReference xmlns="">
<system>Sys2</system>
<partNumber confidence="100">DTR123</partNumber>
<partType>SP</partType>
<UoM>EA</UoM>
<description>test3</description>
<status>CURRENT</status>
<productLifeCycle />
<revision>A</revision>
<TISIndex />
<manager>CMF</manager>
<UNSPSC>3212</UNSPSC>
<category>Pref</category>
<source>S2</source>
<aliasList>
<alias valid="true" confidence="100" reference="Manu">
<partNumber>R413I2</partNumber>
<DnBCode />
<name>KE</name>
<status>S : Pr</status>
<category>MR</category>
</alias>
<alias valid="true" confidence="100" reference="Manu">
<partNumber>PME271Y510MR30</partNumber>
<DnBCode />
<name>KEMET</name>
<status>S : Product maturity</status>
<category>ManufacturerRef</category>
</alias>
<alias valid="true" confidence="100" reference="Legacy">
<partNumber>--</partNumber>
<owner>PLM</owner>
<category>LegacyPartRef</category>
</alias>
</aliasList>
</partReference>
</instance>
<attachments />
</envelope>
</MasterSystem>
<AliasSystem>
<envelope xmlns="">
<headers>
<creationDate xmlns="">2019-04-10</creationDate>
</headers>
<triples />
<instance>
<partReference xmlns="">
<system>ERP</system>
<partNumber confidence="100">DTR02</partNumber>
<partType />
<UoM>EA</UoM>
<mandantStatus>ZB</mandantStatus>
<description />
<status>J5PARTSTATUS2</status>
<productLifeCycle />
<category>pR</category>
<source>S3</source>
<aliasList />
</partReference>
</instance>
<attachments />
</envelope>
</AliasSystem>
</result>
在XML文件中有一个MasterSystem节,并且有多个AliasSystem节。我想存储每个部分(MasterSystem和AliasSystem部分)中的'creationdate','partreference'(所有chlidren)和别名列表。但是我正在尝试以某种方式存储数据,以识别其解析自的部分(定义标准系统或源元素)。
答案 0 :(得分:0)
尝试使用XMl Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication108
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement result = doc.Root;
List<PRecord> records = new List<PRecord>();
List<XElement> systems = result.Elements("MasterSystem").ToList();
systems.AddRange(result.Elements("AliasSystem"));
foreach (XElement system in systems)
{
PRecord newRecord = new PRecord();
records.Add(newRecord);
newRecord.systemType = system.Name.LocalName;
newRecord.creationDate = (DateTime)system.Descendants("creationDate").FirstOrDefault();
XElement partReference = system.Descendants("partReference").FirstOrDefault();
XElement xPartNumber = partReference.Element("partNumber");
newRecord.partNumber = (string)xPartNumber;
newRecord.partNumber_confidence = (int)xPartNumber.Attribute("confidence");
newRecord.partType = (string)partReference.Element("partType");
newRecord.description = (string)partReference.Element("description");
newRecord.revision = (string)partReference.Element("revision");
newRecord.status = (string)partReference.Element("status");
newRecord.category = (string)partReference.Element("category");
newRecord.source = (string)partReference.Element("source");
newRecord.productLifeCycle = (string)partReference.Element("productLifeCycle");
List<XElement> aliases = partReference.Descendants("alias").ToList();
foreach (XElement alias in aliases)
{
if (newRecord.aliases == null) newRecord.aliases = new List<ARecord>();
ARecord newARecord = new ARecord();
newRecord.aliases.Add(newARecord);
newARecord.a_partNumber = (string)alias.Element("partNumber");
newARecord.a_DnBCode = (string)alias.Element("DnBCode");
newARecord.a_ManufName = (string)alias.Element("name");
newARecord.a_category = (string)alias.Element("status");
newARecord.a_status = (string)alias.Element("category");
}
}
}
}
public class PRecord
{
public string systemType { get; internal set; }
public DateTime creationDate { get; internal set; }
public string system { get; internal set; }
public string partNumber { get; internal set; }
public int partNumber_confidence { get; internal set; }
public string partType { get; internal set; }
public string description { get; internal set; }
public string revision { get; internal set; }
public string status { get; internal set; }
public string part_source { get; internal set; }
public string category { get; internal set; }
public string source { get; internal set; }
public string productLifeCycle { get; internal set; }
public List<ARecord> aliases { get; internal set; }
}
public class ARecord
{
public string a_partNumber { get; set; }
public string a_ManufName { get; set; }
public string a_DnBCode { get; set; }
public string a_category { get; set; }
public string a_status { get; set; }
}
}