Solved

XML Data in to Structure

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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