Solved

XML Data in to Structure

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now