简体   繁体   English

复杂/嵌套 XML 读取 C#

[英]Complex/Nested XML Reading in C#

I have been trying to read this XML file however it is complex/nested a good amount compared to the examples I have seen online.我一直在尝试阅读此 XML 文件,但是与我在网上看到的示例相比,它很复杂/嵌套了很多。 I have tried using LINQ and XMLReader with no luck.我试过使用 LINQ 和 XMLReader 没有运气。

LINQ will read each OrderScreen; LINQ 将读取每个 OrderScreen; however, when it comes to the Cell of each OrderScreen it loads all possible Cells into each OrderScreen even if the Cell does not belong to that OrderScreen.然而,当涉及到每个 OrderScreen 的 Cell 时,它会将所有可能的 Cell 加载到每个 OrderScreen 中,即使该 Cell 不属于该 OrderScreen。 I understand why it does it, but I am fairly new to LINQ and most of the examples I see are not this complex and do not really cover this.我理解它为什么这样做,但我对 LINQ 还很陌生,而且我看到的大多数示例都没有这么复杂,也没有真正涵盖这一点。

XMLReader works pretty well but it does not continue reading the next Cell after it completed the reading of one OrderScreen, it just reads the first Cell of the next OrderScreen then assumes it is at the end of the document. XMLReader 工作得很好,但在完成读取一个 OrderScreen 后它不会继续读取下一个 Cell,它只是读取下一个 OrderScreen 的第一个 Cell,然后假定它位于文档的末尾。 I did not include that code because all the searches I have seen people using LINQ over XMLReader.我没有包含该代码,因为我看到人们在 XMLReader 上使用 LINQ 的所有搜索。

XML is below first, most recent LINQ code after that XML 首先是下面,最近的 LINQ 代码在后面

Any help is greatly appreciated!任何帮助是极大的赞赏!

<Screens>
  <DeleteScreens></DeleteScreens>
  <NewScreens>
    <OrderScreen>
      <ScreenNumber></ScreenNumber>
      <Title></Title>
      <NumberOfColumns></NumberOfColumns>
      <OptionScreen></OptionScreen>
      <ShowQuantityButtons></ShowQuantityButtons>
      <PrepSequenceScreen></PrepSequenceScreen>
      <Cell>
        <CellNumber></CellNumber>
        <CellName></CellName>
        <InventoryNumber></InventoryNumber>
        ...more Cell elements..
        <OptionGroup>
          <Type></Type>
          <ScreenNumber></ScreenNumber>
          <Cells></Cells>
        </OptionGroup>
        ...more OptionGroups...
      </Cell>
      ...more Cells...
    </OrderScreen>
    ...more OrderScreens...
  </NewScreens>
  <UpdateMenus>
     <Menu>
      <MenuNumber></MenuNumber>
      <MenuTitle></MenuTitle>
      ...more Menu elements...
    </Menu>
    ...more Menus...
  </UpdateMenus>
<Screens>

XDocument xdoc;
xdoc = XDocument.Load(@"C:\Users\Kwagstaff\Desktop\PMM_3.0\PMM_3.0\XML\Screens.xml");
var ORDERSCREENS = from a in xdoc.Descendants("OrderScreen")
 select new
 {
  ScreenNumber = a.Element("ScreenNumber").Value,
  Title = a.Element("Title").Value,
  NumberOfColumns = a.Element("NumberOfColumns").Value,
  OptionScreen = a.Element("OptionScreen").Value,
  ShowQuantityButtons = a.Element("ShowQuantityButtons").Value,
  PrepSequenceScreen = a.Element("PrepSequenceScreen").Value,
  Cell = from b in xdoc.Descendants("Cell")
  select new
  {
   CellNumber = b.Element("CellNumber"),
  }   
};

In my opinion, the proper way to do that is with entities and decorators, you will need to do some research but as example在我看来,正确的方法是使用实体和装饰器,你需要做一些研究,但作为例子

for something like对于类似的东西

<MyComplexXML>
....
   <xalAddress>...</xalAddress>
   <multiPoint>
     <MultiPoint>...</MultiPoint>
   </multiPoint>
...
</MyComplexXML>

First, you create your classes like this首先,你像这样创建你的类

using System.Xml.Serialization;

namespace MyComplexXML_Model
{
    /// <summary>
    /// Address field for MyComplexXML
    /// </summary>
    public class Address
    {
        /// <summary>
        /// XalAddress
        /// </summary>
        [XmlElement("xalAddress")]
        public XalAddress XalAddress;
        [XmlElement("multiPoint")]
        public MultiPointAddress MultiPointAddress;
    }
}

and

using System.Xml.Serialization;

namespace MyComplexXML_Model
{

    public class MultiPointAddress
    {
        [XmlElement("MultiPoint", Namespace = "http://www.sample.net/sample")]
        public MultiPoint Multipoint;
    }
}

and when your complete hierarchies are in place you can call your root element like this当你的完整层次结构到位时,你可以像这样调用你的根元素

  var ns = new XmlSerializerNamespaces();
    ns.Add("sample", "http://www.sample.net/sample");
    ...

    var ms = new MemoryStream();
    var sw = new StreamWriter(ms);

    //Deserialize from  file 
    var sr = new StreamReader(@"myfile.xml");
    var city = (MyComplexXML)new XmlSerializer(typeof(MyComplexXML)).Deserialize(sr);

Hope this point you in the right direction.希望这能为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM