我有一个非常大的xml文件。这是xml格式的简化版本。
<?xml version='1.0' encoding='UTF-8'?>
<Sender>
<SenderID>571099948</SenderID>
<Sponsors>
<Sponsor>
<SponsorID>TEST01</SponsorID>
<Contracts>
<Contract>
<ContractID>000001</ContractID>
<Member>
<SSN>1111111111</SSN>
<Gender>M</Gender>
<Benefits>
<Benefit BenefitType="AAA">
</Benefit>
<Benefit BenefitType="BBB">
</Benefit>
</Benefits>
</Member>
<Member>
<SSN>4444444444</SSN>
<Gender>F</Gender>
<Benefits>
<Benefit BenefitType="AAA">
</Benefit>
</Benefits>
</Member>
</Contract>
<Contract>
<ContractID>0000002</ContractID>
<Member>
<SSN>2222222222</SSN>
<Gender>F</Gender>
<Benefits>
<Benefit BenefitType="CCC">
</Benefit>
<Benefit BenefitType="DDD">
</Benefit>
</Benefits>
</Member>
</Contract>
<Contract>
<ContractID>0000003</ContractID>
<Member>
<SSN>333333333</SSN>
<Gender>F</Gender>
<Benefits>
<Benefit BenefitType="CCC">
</Benefit>
</Benefits>
</Member>
</Contract>
</Contracts>
</Sponsor>
<Sponsor>
<SponsorID>TEST02</SponsorID>
<Contracts>
<Contract>
<ContractID>0000011</ContractID>
<Member>
<SSN>1111111111</SSN>
<Gender>M</Gender>
<Benefits>
</Benefits>
</Member>
</Contract>
<Contract>
<ContractID>0000002</ContractID>
<Member>
<SSN>2222222222</SSN>
<Gender>F</Gender>
<Benefits>
</Benefits>
</Member>
</Contract>
</Contracts>
</Sponsor>
</Sponsors>
</Sender>
我想获取合约节点的所有信息,以及来自父节点的SponsorID。以下是使用XmlReader部分读取xml文件的代码:
static IEnumerable<XElement> SimpleStreamAxis(string inputUrl, string elementName)
{
using (XmlReader reader = XmlReader.Create(inputUrl))
{
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == elementName)
{
XElement el = XNode.ReadFrom(reader) as XElement;
if (el != null)
{
yield return el;
}
}
}
}
}
}
这是问题所在。我无法使用它,因为整个赞助商树可能对于记忆来说太大了。
var sponsor = SimpleStreamAxis(file, "Sponsor");
我也不能使用它,因为我不能只用Contract节点信息告诉SponsorID。
var contract = SimpleStreamAxis(file, "Contract");
有没有办法可以在赞助商中阅读SponsorID,向前移动光标,阅读本赞助商下的所有合约节点,然后转到下一个赞助商并阅读SponsorID及其合约节点等等?
答案 0 :(得分:1)
是的,可以假设SponsorID
始终位于Contract
个节点之前。
基本思路是阅读XML文件,直到找到具有所需名称"SponsorID"
或"Contract"
的元素,然后将它们用于更高的处理
public static IEnumerable<XElement> StreamNamedElements(XmlReader reader, IEnumerable<XName> names)
{
var nameSet = new HashSet<XName>(names);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && nameSet.Contains(XName.Get(reader.LocalName, reader.NamespaceURI)))
{
XElement el = XNode.ReadFrom(reader) as XElement;
if (el != null)
yield return el;
}
}
}
如果SponsorID
始终存在且位于Contract
之前,则会正确枚举这些元素。但是,如果赞助商ID丢失或无序,则可能会获得前一个赞助商的赞助商ID。可以通过限制每个&#34; SponsorID
&#34;的范围来捕获此错误。到含有&#34; Sponsor
&#34;元素使用ReadSubtree()
:
public static IEnumerable<XmlReader> StreamNamedSubtrees(XmlReader reader, IEnumerable<XName> names)
{
var nameSet = new HashSet<XName>(names);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && nameSet.Contains(XName.Get(reader.LocalName, reader.NamespaceURI)))
{
var subReader = reader.ReadSubtree();
yield return subReader;
((IDisposable)subReader).Dispose(); // Be sure to advance to the end of the subtree if the caller did not.
}
}
}
然后使用它:
using (var sr = new StringReader(xml))
using (var reader = XmlReader.Create(sr))
{
foreach (var subReader in StreamNamedSubtrees(reader, new[] { (XName)"Sponsor" }))
{
XElement sponsorID = null;
foreach (var el in StreamNamedElements(subReader, new[] { (XName)"SponsorID", (XName)"Contract" }))
{
if (el.Name == "SponsorID")
{
sponsorID = el;
}
else if (el.Name == "Contract")
{
if (sponsorID == null)
throw new InvalidOperationException();
// Example "higher processing"
Debug.WriteLine(string.Format("{0}: {1}", sponsorID.Value, el.ToString()));
}
}
}
}
答案 1 :(得分:1)
试试这个:
using (XmlReader xmlReader = XmlReader.Create("file.xml"))
{
while (xmlReader.Read())
{
if (xmlReader.ReadToFollowing("SponsorID"))
{
string sponsorId = xmlReader.ReadElementContentAsString();
// process SponsorID
Console.WriteLine(sponsorId);
if (xmlReader.ReadToFollowing("Contract"))
{
do
{
XmlReader contractSubtree = xmlReader.ReadSubtree();
XElement contractElement = XElement.Load(contractSubtree);
// process Contract
Console.WriteLine(contractElement.Element("ContractID"));
} while (xmlReader.ReadToNextSibling("Contract"));
}
}
}
}