gpt4 book ai didi

c# - 将 XML 解析为列表

转载 作者:行者123 更新时间:2023-11-30 20:46:38 24 4
gpt4 key购买 nike

我有一个非常复杂的 XML,我已经能够解析其中的大部分内容,但是我遇到了一棵让我难过的树,我担心我正在变得比它需要的更难。这是我指的 XML。

<Codes>
<CustomFieldValueSet name="Account" label="Account" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>7200</Value>
<Description>General Supplies</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>7200</Value>
<Description>General Supplies</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>7200</Value>
<Description>General Supplies</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Activity" label="Activity" distributionType="PercentOfPrice" />
<CustomFieldValueSet name="Chart" label="Chart" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>T</Value>
<Description>University</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>T</Value>
<Description>University</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>T</Value>
<Description>University</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Fund" label="Fund" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>360806</Value>
<Description>National Institutes of Health</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>360903</Value>
<Description>National Institutes of Health</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>360957</Value>
<Description>National Institutes of Health</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Program" label="Program" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>02</Value>
<Description>Research</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>02</Value>
<Description>Research</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>02</Value>
<Description>Research</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Location" label="Location" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>015</Value>
<Description>Biology - Life Science</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>015</Value>
<Description>Biology - Life Science</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>015</Value>
<Description>Biology - Life Science</Description>
</CustomFieldValue>
</CustomFieldValueSet>
<CustomFieldValueSet name="Organization" label="Organization" distributionType="PercentOfPrice">
<CustomFieldValue distributionValue="10.00" splitindex="0">
<Value>04400</Value>
<Description>TUSM:Neuroscience</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="1">
<Value>04400</Value>
<Description>TUSM:Neuroscience</Description>
</CustomFieldValue>
<CustomFieldValue distributionValue="45.00" splitindex="2">
<Value>04400</Value>
<Description>TUSM:Neuroscience</Description>
</CustomFieldValue>
</CustomFieldValueSet>
</Codes>

我正试图得到一个看起来像这样的列表。

Account distributionType   Activity   distributionValue  Fund
7200 PercentOfPrice "" 10 360806
7200 PercentOfPrice "" 45 360903
7200 PercentOfPrice "" 45 360957

等...

我编写的代码看起来像这样。这是一个片段。请注意,我想我把这个复杂化了。

if (tagName == "Codes")
{
// Create another reader that contains just the accounting elements.
XmlReader inner = reader.ReadSubtree();
//inner.ReadToDescendant("Codes");
//printOutXML(inner);
while (inner.Read())
{
switch (inner.NodeType)
{
//walk down the xml hiearchy then simply fill in the values.
case XmlNodeType.Element:

switch (reader.Name)
{
case "CustomFieldValueSet":
//get the attribute that we are currently working with such as account and
innerTagName=inner.GetAttribute("name");

// activity and location can potentially be blank therefore i will check here if it is
//and if it is i will immediate assign the activity list a set of empty quotes.
if (innerTagName == "Activity")
{
if (inner.IsEmptyElement)
{ //quickly put fillers in .
for (int i = 0; i < thisInvoice.account.Count; i++)
{
thisInvoice.activity.Add("");
}
}
}

if (innerTagName == "Location")
{
if (inner.IsEmptyElement)
{ //quickly put fillers in .
for (int i = 0; i < thisInvoice.account.Count; i++)
{
thisInvoice.location.Add("");
}
//thisInvoice.activity.Add("");
}
}

if (null == inner.GetAttribute("distributionType"))
{
distType = null;
}
else if
(distributionSwitch == false)
{
thisInvoice.distributionType.Add(inner.GetAttribute("distributionType") ?? "");
distType = inner.GetAttribute("distributionType") ?? "";
}
//Console.WriteLine(inner.Value);
//Console.WriteLine(inner.Name);
break;

case "CustomFieldValue":
if(null == inner.GetAttribute("distributionValue"))
//thisInvoice.distributionValue.Add(inner.GetAttribute("distributionValue") ?? "");
{/*do nothing*/}
else if
(distributionSwitch == false)
{
thisInvoice.distributionValue.Add(inner.GetAttribute("distributionValue") ?? "");
}
//check the length of the current distribution if the lenght is less than the curren distribution value
// then we must then add the values to the new location.
if (thisInvoice.distributionValue.Count > thisInvoice.distributionType.Count)
{
for (int i = 0; i < thisInvoice.distributionValue.Count - thisInvoice.distributionType.Count; i++)
{
thisInvoice.distributionType.Add(distType);
}



}

break;

case "Value":
// XmlNodeType.Text
if (innerTagName == "Account"/*&& inner.NodeType ==XmlNodeType.Text*/)
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.account.Add(inner.Value);
}


if (innerTagName == "Activity")
{
// activitiy is not a mandartory field so it could be empty therefore we need
// to check if its a self closing tag and if it is then we need to assign and
if (inner.IsEmptyElement)
{
thisInvoice.activity.Add("");
}
else
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.activity.Add(inner.Value);
}
}

if (innerTagName == "Location")
{
if (inner.IsEmptyElement)
{
thisInvoice.location.Add("");
}
else
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.location.Add(inner.Value);
}
}

if (innerTagName == "Fund")
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.fund.Add(inner.Value);
}

if (innerTagName == "Organization")
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.org.Add(inner.Value);
}

if (innerTagName == "Program")
{
inner.MoveToContent();// move to the text
inner.Read();
thisInvoice.prog.Add(inner.Value);
}

break;



}//end switch
break;//brake the outside case.
case XmlNodeType.EndElement:
if (inner.Name == "CustomFieldValueSet" || inner.Value == "CustomFieldValueSet")
{
distributionSwitch = true;
Console.WriteLine(reader.Value);
Console.WriteLine(reader.Name);
}
if (inner.Name == "Codes")
{
distributionSwitch = false;
distType = null;
inner.Close();
}

break;
}//end switch
}//end while
}//end the if;

在标签 distributionType 的情况下,我需要使列表长度与帐户列表一样长,所以换句话说,一旦我将它放在变量上,我就需要将它用作填充符,以使分发类型列表为大如帐户列表。我无法想象没有更简单的方法可以做到这一点我一直在看 linq to xml 但它没有多大意义。我很想听听你们中的一些专家将如何解决这个问题。我正在尝试用更少的代码组合一个优雅的解决方案。任何帮助将不胜感激。

最佳答案

如评论部分所述,Mihai 的替代方案使用 LINQ to XML 的解决方案,您还可以使用预定义的类结构将 XML 反序列化为类型化的类和属性。

这样做的好处是您将拥有一个表示 XML 的对象(希望如此)并允许您更轻松地处理 XML 中的数据

使用提供的 XML 示例并使用 Visual Studio 中的 Edit -> Paste Special -> Paste XML as Classes 菜单选项,您将得到一个类似于下面的类结构(这个已经被改进了一点以便于阅读)

using System.Xml.Serialization;

[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Codes
{
[XmlElementAttribute("CustomFieldValueSet")]
public List<CodesCustomFieldValueSet> CustomFieldValueSet { get; set; }
}

[XmlTypeAttribute(AnonymousType = true)]
public partial class CodesCustomFieldValueSet
{
[XmlElementAttribute("CustomFieldValue")]
public List<CodesCustomFieldValueSetCustomFieldValue> CustomFieldValue { get; set; }

[XmlAttributeAttribute(AttributeName="name")]
public string Name { get; set; }

[XmlAttributeAttribute(AttributeName = "label")]
public string Label { get; set; }

[XmlAttributeAttribute(AttributeName = "distributionType")]
public string DistributionType { get; set; }
}

[XmlTypeAttribute(AnonymousType = true)]
public partial class CodesCustomFieldValueSetCustomFieldValue
{
public string Value { get; set; }

public string Description { get; set; }

[XmlAttributeAttribute(AttributeName = "distributionValue")]
public decimal DistributionValue { get; set; }

[XmlAttributeAttribute(AttributeName = "splitindex")]
public byte SplitIndex { get; set; }
}

有了这个类结构,您就可以用下面的代码反序列化您的 XML
(其中 txtInput.Text 是我用来保存示例 XML 数据的文本框)

XmlSerializer serializer = new XmlSerializer(typeof(Codes));
Codes codesInput = serializer.Deserialize(new StringReader(txtInput.Text)) as Codes;

if (codesInput != null)
{
// Do something with the data
}

注意:
根据您想要的输出和您提供的示例 XML 的结构,您需要将反序列化对象中的信息转换为您想要的内容/方式,为此我建议创建一个额外的类结构,结合一个 List<T> , 以保存所需输出中显示的所有信息。

如果您控制 XML 的结构并以更好的方式构造它,使其比当前的结构更易于解释,那就更好了,因为看起来每个 CustomFieldValueSet 之间的链接是splitindex ,这是子节点的一个属性,这使它变得非常复杂。

进一步阅读 XML 序列化:
MSDN: Introducing XML Serialization
The XmlSerializer Class

关于c# - 将 XML 解析为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26631139/

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