0

我是 xml 新手,需要使用类似于以下示例的 xml 循环浏览网页。我需要存储每个元素?将 xml 转换为变量,因此我可以为每个唯一 ID 创建一个包含一行的列表。id:101,描述:汽车,名称:Bon Bon,街道:123 W Place St,State 等。我相信 id 将是一个属性?其余的值是innerxml?

我一直在网上搜索信息,但未能完成很多工作。任何帮助将不胜感激,因为我起步缓慢。

 <Master>
   <Dealers>
     <Dealer id="101">
       <Description>Auto</Description>
       <Name>Bon Bon Motors</Name>
       <Address>
         <Street>123 W Place St</Street>
         <City>Chicago</City>
         <State>IL</State>
         <Zip>82453</Zip>
       </Address>
       <PhoneNo>5451252222</PhoneNo>
     </Dealer> 
     <Dealer id-"102">
      ...
      ...
      ...
     </Dealer>
   </Dealers>
 </Master>


 XmlDocument doc = new XmlDocument();
 doc.Load("http://data.xml");
 XmlElement root = doc.DocumentElement;
 List<SomeType> list1 = new List<SomeType>();
 if (root.HasAttribute("id"))
 {
      foreach (.... )
      { 
          int pk = root.Attributes["id"].Value;
          string description = ...
          string name ...

          list1.Add(pk);
          list1.Add(description);
          ....
      }
 }
4

4 回答 4

3
static void Main(string[] args)
{
    var xml = @"<Master>
                    <Dealers>
                        <Dealer id=""101"">
                        <Description>Auto</Description>
                        <Name>Bon Bon Motors</Name>
                        <Address>
                            <Street>123 W Place St</Street>
                            <City>Chicago</City>
                            <State>IL</State>
                            <Zip>82453</Zip>
                        </Address>
                        <PhoneNo>5451252222</PhoneNo>
                        </Dealer> 
                        <Dealer id=""102"">
                        <Description>Auto</Description>
                        <Name>Bon Bon Motors</Name>
                        <Address>
                            <Street>123 W Place St</Street>
                            <City>Chicago</City>
                            <State>IL</State>
                            <Zip>82453</Zip>
                        </Address>
                        <PhoneNo>5451252222</PhoneNo>
                        </Dealer>
                    <Dealer id=""103"">
                        <Description>Auto</Description>
                        <Name>Bon Bon Motors</Name>
                        <Address>
                            <Street>123 W Place St</Street>
                            <City>Chicago</City>
                            <State>IL</State>
                            <Zip>82453</Zip>
                        </Address>
                        <PhoneNo>5451252222</PhoneNo>
                        </Dealer> 
                    </Dealers>
                    </Master>";

    var results = XDocument.Parse(xml).Root // Master
                           .Descendants("Dealer")
                           .Select(dealer => new
                               {
                                   Id = dealer.Attribute("id").Value,
                                   Description = dealer.Element("Description").Value,
                                   Name = dealer.Element("Name").Value,
                                   Street = String.Join(", ", dealer.Element("Address")
                                                                   .Elements() // Street, City, State, Zip
                                                                   .Select(element => element.Value)
                                                                   .ToArray())
                               }).ToList();

    results.ForEach(result => Console.WriteLine("Id: {0}; Description: {1}; Name: {2}; Address: {3}", 
                                            result.Id, result.Description, result.Name, result.Street));

    Console.Read();
}

打印到控制台:

Id: 101; Description: Auto; Name: Bon Bon Motors; Address: 123 W Place St, Chicago, IL, 82453   
Id: 102; Description: Auto; Name: Bon Bon Motors; Address: 123 W Place St, Chicago, IL, 82453
Id: 103; Description: Auto; Name: Bon Bon Motors; Address: 123 W Place St, Chicago, IL, 82453
于 2013-06-25T04:03:12.113 回答
1

这是更多 LINQified 和简化的方法。我刚刚写了它并测试了 i var

        var xmlDocument = XDocument.Load("C:\\TEMP\\test.xml");
        var nodesList = from xmlNode in xmlDocument.Descendants("Dealer")
                        select
                            new
                                {
                                    Id = xmlNode.Attribute("id").Value,
                                    Description = xmlNode.Descendants("Description").ElementAt(0).Value,
                                    Name = xmlNode.Descendants("Name").ElementAt(0).Value,
                                    Address = new
                                        {
                                            Street = xmlNode.Descendants("Address").Descendants("Street").ElementAt(0).Value,
                                            City = xmlNode.Descendants("Address").Descendants("City").ElementAt(0).Value,
                                            State = xmlNode.Descendants("Address").Descendants("State").ElementAt(0).Value,
                                            ZipCode = xmlNode.Descendants("Address").Descendants("Zip").ElementAt(0).Value
                                        },
                                    PhoneNumber = xmlNode.Descendants("PhoneNo").ElementAt(0).Value
                                }           
            ;

        foreach (var node in nodesList)
        {
            Console.WriteLine(node.Id);
        }

        Console.ReadKey();
于 2013-06-25T04:04:23.397 回答
1

这就像你想要的:

class Program
{
    static void Main(string[] args)
    {
        var doc = new XmlDocument();
        doc.Load(@"..\..\input.xml");

        var container = doc.DocumentElement
            .GetElementsByTagName("Dealers")
            .OfType<XmlElement>()
            .FirstOrDefault();

        if (container == null) return;

        var dealers = container
            .GetElementsByTagName("Dealer")
            .OfType<XmlElement>();

        foreach (var dealer in dealers)
        {
            var dealerId = dealer.GetAttribute("id");
            Console.Write(dealerId + " - ");

            var descrip = dealer.GetElementsByTagName("Description").OfType<XmlElement>().FirstOrDefault();
            if (descrip != null)
                Console.WriteLine(descrip.InnerText);

            // etc...
        }
        Console.ReadLine();
    }
}
于 2013-06-25T03:02:33.403 回答
0

如果您知道 XML 文件的架构,则可以创建类并将 XML 反序列化为对象,然后您就可以最大程度地灵活地处理它们。

在您的示例中,您有三个类,地址、经销商和大师。反序列化后会得到一个包含List属性的Master对象,每个Dealer对象都包含相关信息。

于 2013-06-25T02:59:29.737 回答