通过C#中的XMLReader读取多级XML

时间:2014-08-09 11:10:12

标签: c# xml xmlreader

我正在尝试阅读,并使用以下结构处理XML。

<?xml version="1.0" encoding="utf-8"?>
<print_format last_called_time="10-08-2014 00:00:00">
  <print_set>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="bold" font="Verdana" height="20">Print 1</line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="normal" font="Verdana" height="20">Sample Print 1 for test.</line>
    <line align="center" weight="normal" font="Verdana" height="20">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed placerat aliquam odio eget dapibus. </line>
    <line align="center" weight="normal" font="Verdana" height="20">Sed pharetra mauris luctus purus mattis porta. </line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="normal" font="Verdana" height="20">Print Ends</line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
  </print_set>
  <print_set>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="bold" font="Verdana" height="20">Print 2</line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="normal" font="Verdana" height="20">Sample Print 2 for test.</line>
    <line align="center" weight="normal" font="Verdana" height="20">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed placerat aliquam odio eget dapibus. </line>
    <line align="center" weight="normal" font="Verdana" height="20">Sed pharetra mauris luctus purus mattis porta. </line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="normal" font="Verdana" height="20">Print Ends</line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
  </print_set>
  <print_set>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="bold" font="Verdana" height="20">Print 3</line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="normal" font="Verdana" height="20">Sample Print 3 for test.</line>
    <line align="center" weight="normal" font="Verdana" height="20">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed placerat aliquam odio eget dapibus. </line>
    <line align="center" weight="normal" font="Verdana" height="20">Sed pharetra mauris luctus purus mattis porta. </line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="normal" font="Verdana" height="20">Print Ends</line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
  </print_set>
  <print_set>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="bold" font="Verdana" height="20">Print 4</line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="normal" font="Verdana" height="20">Sample Print 4 for test.</line>
    <line align="center" weight="normal" font="Verdana" height="20">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed placerat aliquam odio eget dapibus. </line>
    <line align="center" weight="normal" font="Verdana" height="20">Sed pharetra mauris luctus purus mattis porta. </line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
    <line align="center" weight="normal" font="Verdana" height="20">Print Ends</line>
    <line align="center" weight="normal" font="Verdana" height="20">-------------------------------</line>
  </print_set>
</print_format>

每个打印套件都需要单独打印。由于应用程序将在.NET 2.0上运行,我想避免使用LINQ。

请帮助为此结构编写循环。我需要阅读单独的'print_set',其中包含每个'line'的属性。

感谢您提出建议我现在通过使用以下代码获得解决方案,这可能有助于完整:对其他人

using (XmlReader reader = XmlReader.Create(xml_url))  
            {
               // Console.WriteLine("Output Using XMLReader:");  
                int i = 1;   
                while (reader.Read()) 
                {                   
                    switch(reader.NodeType){
                        //If Element, check which element
                        case XmlNodeType.Element:
                            switch(reader.Name){
                            //If Print_format, get modified time
                                case "print_format":                                    
                                    Console.WriteLine("Start of Print Fomrat");  
                                    Console.WriteLine("Modified Time:" + reader.GetAttribute("sale_modified_time"));
                                    break; 
                                //If print_set start print variable
                                case "print_set":
                                    Console.WriteLine("Print Set Start:"+i);
                                    break;
                                //If line, print line details 
                                case "line":                                                                     
                                    if (reader.GetAttribute("height") == null)
                                    {
                                        Console.WriteLine("Height is null");
                                    }
                                    if (reader.GetAttribute("font") == null)
                                    {
                                        Console.WriteLine("Font is null");
                                    }
                                    if (reader.GetAttribute("weight") == null)
                                    {
                                        Console.WriteLine("Weight is null");
                                    }
                                    if (reader.GetAttribute("align") == null) 
                                    {
                                        Console.WriteLine("Align is null");
                                    }
                                    Console.WriteLine(reader.GetAttribute("align") + "  " + reader.GetAttribute("weight") + " " + reader.GetAttribute("font") + "  " + reader.GetAttribute("height") + "  " + reader.ReadString());  
                                    break;    
                            }
                            break; 
                        case XmlNodeType.EndElement:
                            switch (reader.Name)
                            {
                                //If End Element, check if it's Print Set
                                //If reader.Name is print_set, the send to printer, and end the print page.
                                //Else do nothing
                                case "print_set":
                                    Console.WriteLine("End of Print set:"+i);
                                    i++;
                                    Console.WriteLine("--------------------------------------");
                                    break;
                                case "print_format":
                                    Console.WriteLine("End of print format");
                                    break;
                            }
                            break;


                    }



                }
            }

如果您尝试从xml字符串中读取数据,则可以使用XMLtext reader 如下所示:

 using (XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(server_data)))   
                {
}

0 个答案:

没有答案