简体   繁体   中英

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. I have tried using LINQ and XMLReader with no luck.

LINQ will read each 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. 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.

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. I did not include that code because all the searches I have seen people using LINQ over XMLReader.

XML is below first, most recent LINQ code after that

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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