Solved

XML Data in to Structure

Posted on 2004-10-12
5
212 Views
Last Modified: 2010-05-18
I have an xml file and want to read the xml in to array of structure, for example

xml file looks like this:-
<MarkupItems>
 <items>
   </TagValue>
   </TagID>
   <InputOffset>
      </OffsetValue>
      </OffsetLength>
   </InputOffset>
   <OutputOffset>
      </OffsetValue>
      </OffsetLength>
   </OutputOffset>
 </items>
 <items>
   </TagValue>
   </TagID>
   <InputOffset>
      </OffsetValue>
      </OffsetLength>
   </InputOffset>
   <OutputOffset>
      </OffsetValue>
      </OffsetLength>
   </OutputOffset>
  </items>
</MarkupItems>

my structure looks like this:-
public struct Offset
{
      public ulong OffsetValue;
      public ulong OffsetLength;
}
public struct TagItem
{
      public Offset InputOffset;
      public Offset OutputOffset;
      public string TagValue;
      public string TagID;
}
public struct MarkUpItems
{
           TagItem[] _items;
}

now i want to read the xml file in to the markupitems structure.
0
Comment
Question by:angireddy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
Razzie_ earned 500 total points
ID: 12288068
This sure ain't pretty code, but I wrote it real quick so please, at least it does the job :)

-------------------------------------------------------------------------------

Offset inputOffset;
Offset outputOffset;
TagItem tagItem;
MarkUpItems markupItems = new MarkUpItems();
                                    
XmlDocument xmlDoc = new XmlDocument();
                  
try
{
      // Load your xml string using xmlDoc.Load() for loading a file or xmlDoc.LoadXml() for loading an xml string

      XmlNodeList itemNodes = xmlDoc.SelectNodes("MarkupItems/items");
      markupItems._items = new TagItem[itemNodes.Count];

      for(int i=0;i<itemNodes.Count;i++)
      {
            inputOffset = new Offset();
            inputOffset.OffsetValue = Convert.ToUInt64(itemNodes[i].SelectSingleNode("InputOffset/OffsetValue").InnerText);
            inputOffset.OffsetLength = Convert.ToUInt64(itemNodes[i].SelectSingleNode("InputOffset/OffsetLength").InnerText);
            outputOffset = new Offset();
            outputOffset.OffsetValue = Convert.ToUInt64(itemNodes[i].SelectSingleNode("OutputOffset/OffsetValue").InnerText);
            outputOffset.OffsetLength = Convert.ToUInt64(itemNodes[i].SelectSingleNode("OutputOffset/OffsetLength").InnerText);
            tagItem = new TagItem();
            tagItem.InputOffset = inputOffset;
            tagItem.OutputOffset = outputOffset;
            tagItem.TagID = itemNodes[i].SelectSingleNode("TagID").InnerText;
            tagItem.TagValue = itemNodes[i].SelectSingleNode("TagValue").InnerText;
            markupItems._items[i] = tagItem;
      }
}
catch(XmlException ex)
{
      Debug.WriteLine(ex.Message);
}

-----------------------------------------------------------------

No error checking for correct input, so you'd have to add that. If you get the idea you might want to clean the code up a bit.
And btw, a single empty node in Xml has the following syntax '<TagID/>' instead of '</TagID>'.

HTH,

Razzie
0
 

Author Comment

by:angireddy
ID: 12290160
Hello Mr.Razzie,

its saying Object Reference not set to an instance of an object for the line

inputOffset.OffsetValue = Convert.ToUInt64(itemNodes[i].SelectSingleNode("InputOffset/OffsetValue").InnerText);

Reddy
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12290239
That is correct. As I stated in my post, there is no error checking for correct input. You'd have to check if the Nodes aren't empty before converting them to a long valuetype. (if ......InnerText != String.Empty) or something, etc :)
0
 

Author Comment

by:angireddy
ID: 12291254
Hi,

how to write back the marupItems back to the xml file using the same example as above.

thanking you
reddy
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12291786
The correct way to do this would be by rebuilding the XmlDocument. The complete code is a lot of work but below is how it is done. Just fill in the missing data:

---------------------------------------------------------------------------------------------
// Create all the nodes
XmlElement xmlElementRoot = xmlDoc.CreateElement("MarkUpItems");
XmlElement xmlElementItems = xmlDoc.CreateElement("items");
....
XmlElement xmlElementOL = xmlDoc.CreateElement("OffsetLength");

// Create the text for the nodes. Obtain the data from the struct.
XmlText xmlTextOL = xmlDoc.CreateTextNode(tagItem.OutputOffset.OffsetLength);
....
etc

// Append the text elements to the nodes
xmlElementOL .AppendChild(xmlTextOL);
...
etc

// Connect the nodes to eachother. Start with root node, its childeren, THEIR childeren, etc.
xmlDoc.AppendChild(xmlElementRoot);
xmlElementRoot.AppendChild(xmlElementItems);
xmlElementItems.AppendChild(xmlElementOutputOffset);
...
etc

----------------------------------------------------------------------

Use a foreach loop like foreach(TagItem t in markUpItems._items) to get all items nodes.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question