我想解析以下作为String输入的XML文件。
<leadtools_form_processing_fields>
<pages>
<page number="1" dpix="300" dpiy="300">
<fields>
<field type="Leadtools.Forms.Processing.TextFormField, Leadtools.Forms.Processing>
<values>
<value name="LastName" />
<value bounds="232, 875, 736, 76 Pixel" />
<value dropOut="None" />
<value regex_pattern="" />
<value ocr_text_type="FlowText" />
<value enable_icr="False" />
<value enable_ocr="True" />
<value text_field_type="Character" />
</values>
</field>
<field type="Leadtools.Forms.Processing.TextFormField, Leadtools.Forms.Processing>
<values>
<value name="MiddleName" />
<value bounds="1112, 875, 475, 76 Pixel" />
<value dropOut="None" />
<value regex_pattern="" />
<value ocr_text_type="FlowText" />
<value enable_icr="False" />
<value enable_ocr="True" />
<value text_field_type="Character" />
</values>
</field>
</fields>
</page>
</pages>
</leadtools_form_processing_fields>
我猜XMLReader提供的效率远高于XMLDocument。如何在&#39;值中获取元素的值?元素并使用XMLReader和foreach解析其他元素。
我试过这个:(种伪代码)
using (XmlReader reader = XmlReader.Create(xmlData))
{
while (reader.Read())
{
reader.ReadToFollowing("pages");
foreach (XmlElement Page in Pages)
{
pageNumber = Page.GetAttribute("number");
reader.ReadToFollowing("fields");
foreach (XmlElement field in fields)
{
type = Page.GetAttribute("type");
reader.ReadToFollowing("values");
foreach (XmlElement value in values)
{
name = value.GetAttribute("name");
string bounds = value.GetAttribute("bounds");
string boundValues[] = bounds.Split(,);
x = boundValues[0];
y = boundValues[1];
width = boundValues[2];
height = boundValues[3];
}
formFields.Add(new FormField() { Name = name, X = x, Y = y, Width = width, Height = height, PageNumber = pageNumber, FormFieldType = type });
}
}
谢谢!
答案 0 :(得分:0)
XmlReader
无法使用foreach循环。您可以使用类似于以下内容的代码:
using (XmlReader reader = XmlReader.Create(filepath))
{
while (reader.ReadToFollowing("page"))
{
pageNumber = reader.GetAttribute("number");
while (reader.ReadToFollowing("field"))
{
type = reader.GetAttribute("type");
reader.ReadToFollowing("value");
name = reader.GetAttribute("name");
reader.ReadToFollowing("value");
string bounds = reader.GetAttribute("bounds");
string[] boundValues = bounds.Split(' ');
x = boundValues[0].Trim(',');
y = boundValues[1].Trim(',');
width = boundValues[2].Trim(',');
height = boundValues[3];
formFields.Add(new FormField() { Name = name, X = x, Y = y, Width = width, Height = height, PageNumber = pageNumber, FormFieldType = type });
}
}
}
在while循环中使用ReadToFollowing
方法迭代节点。