Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Holymolymo · Mar 20, 2012 at 06:50 PM · xmlxmlserializer

Need help in appending to Xml document

hello everyone,

I've been having a lot of difficulty in getting this to work. I've scoured the internet for a solution, but couldn't find any.

Here is my problem:

This is the XML document that I need to append to.

  <?xml version="1.0" encoding="utf-8"?>
 <Participants>
   <Participant>
     <Price>175000</Price>
     <Choice>Neither</Choice>
     <Choice>1</Choice>
     <Choice>Neither</Choice>
     <Municipality>NDG</Municipality>
     <HouseholdSize>4</HouseholdSize>
     <Gender>Female</Gender>
     <Age>55-64</Age>
     <Employment>student and employed</Employment>
     <Income>75000-99999</Income>
   </Participant>
   <Participant>
     <Price>225000</Price>
   </Participant>
 </Participants>

I need to append to the second Participant and I have the following code to do so.

 if(File.Exists("Information.xml") && PlayerPrefs.GetInt("Participant size")>1)
   {
  
  var tracker=PlayerPrefs.GetInt("Participant size");
         xmlRead.WhitespaceHandling=WhitespaceHandling.None;
         xmlRead.MoveToContent();
         xmlRead.Read();
     for(var i=1;i<tracker;i++)
     {
         xmlRead.Skip();
         xmlRead.Skip();
     }
   doc.Load(xmlRead);
   var elem =doc.CreateElement("Choice");
   elem.InnerText=Choice();
   doc.DocumentElement.AppendChild(elem);
 
    xmlRead.Close();
    doc.Save("Information.xml");
   

 }

However whenever it gets to Doc.Load it gives me the following error:

NullReferenceException: Unexpected node type EndElement. System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) selectNeighborhood.OnGUI () (at Assets/scripts/GUI Scripts/selectNeighborhood.js:145)

Does anyone know why?

Additional info if needed:

xmlRead is defined as:

 var readerSettings = new XmlReaderSettings();
  readerSettings.IgnoreWhitespace = true;
  readerSettings.IgnoreComments = true;
 var xmlRead= XmlReader.Create("Information.xml",readerSettings);

Thanks guys and gals,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Holymolymo · Mar 20, 2012 at 10:45 PM

Hi Dave A.

I gave up taking that approach and went in another direction which ended up being a better solution and I was able to get rid of a lot of redundant code.

The Skip() was being used to place itself to the last Participant. In this case the second participant. After extensive testing I discovered that it was the only way of placing the reader in the right place if that makes any sense.

Do know that it was my first time using Xml reader, writer and document ,so in hindsight it probably was a good idea to redo it after experimenting for a bit.

Thanks anyways!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Mar 20, 2012 at 10:49 PM 0
Share

You should use the 'comment' feature to respond to specific answers. Glad to hear it's working.

avatar image
1

Answer by Karsnen_2 · Jul 11, 2012 at 08:25 PM

You might like to check this blog out.

It is pretty much comprehensive.

XML <-> Unity3d

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by DaveA · Mar 20, 2012 at 07:27 PM

Several thoughts:

You don't need to parse and build XML with XmlReader to do this. You could read the file to the tag and use string manipulation (for example) to insert what you need.

What is intended here:

  for(var i=1;i<tracker;i++)
     {
        xmlRead.Skip();
        xmlRead.Skip();
     }

If that's supposed to skip the second Participant, why two skips, why start a 1 (not 0)?

You could also use doc.FirstChild then NextSibling to skip Participants. In that case, read the whole doc (don't bother with Skip)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

"Root Element Missing: XML Exception" when trying to load data from XML file 2 Answers

Serialize a class that extends as a serializable class? 0 Answers

XML Serialization issues 0 Answers

loading XML quiz 1 Answer

XML Serialization not working on WebGL 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges