Solved

XML Data in to Structure

Posted on 2004-10-12
5
209 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MediaHelp 4 26
Moving code from one TFS branch to another 3 33
How to make a Gridview column hidden (visibility false) in the HTML code? 2 20
tableview is not updating 1 22
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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