Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XML Data in to Structure

Posted on 2004-10-12
5
Medium Priority
?
215 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

877 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