Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

c# - loading from xml file

i am trying loading and saving data by Robert Harvey code in this topic. i can save. but load process is not success full.

i have tried :

var list = XmlHelper.FromXmlFile<List<Item>>(@"c:folderfile.xml");

i did not find the correct namespase for Item

var list = XmlHelper.FromXmlFile<List<Array>>(@"c:folderfile.xml");
{"Object reference not set to an instance of an object."}
var list = XmlHelper.FromXmlFile<List<ArrayList>>(@"c:folderfile.xml");
{"<ArrayOfAnyType xmlns=''> was not expected."}
var list = XmlHelper.FromXmlFile<List<Double>>(@"c:folderfile.xml");
{"<ArrayOfAnyType xmlns=''> was not expected."}

but all of them have error which listed below them.

i want to retrieve those number in form of arraylist or double[];

the XML content : enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First Load the Document:

var doc = XDocument.Load("c:somefile.xml");

Then you can access the Elements with

XElement xe = doc.Element("Name of the Element");

If you got more than one Element with the same name you can get them with:

IEnumerable<XElement> xe = doc.Elements("Name of the Element");

You can Access Attributes kind of similar:

XAttribute xa = doc.Attribute("Name of the Attribute");

and

IEnumerable<XAttribute> xa = doc.Attributes("name");

don't forget to always do null checks.

I hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...