C#从XML文件中读取

时间:2016-02-09 10:10:46

标签: c# xml

我正在尝试使用WPF中的按钮在C#中读取XML。 我有这个XML文件:

@IBOutlet weak var fbLoginView: FBSDKLoginButton!

override func viewDidLoad() {
    super.viewDidLoad()

    if(FBSDKAccessToken.currentAccessToken() == nil){
        print("Not Logged ")
    }
    else{
        print("Logged In")
    }
    fbLoginView.readPermissions = ["public_profile","email","user_birthday"]
    fbLoginView.delegate = self

    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
    if error == nil
    {
        print("login complete")
        let request = FBSDKGraphRequest(graphPath:"me", parameters:["fields":"name,gender,birthday,first_name,last_name,email"])
        request.startWithCompletionHandler
            {
                (connection, result, error) in
                if error != nil
                {
                    print ("error \(error)")
                }
                else if let userData = result as? NSDictionary
                {

                   var frstname = userData["first_name"] as? String
                   var lastname =  userData["last_name"] as? String
                   var gender = userData["gender"] as? String
                   var birthday = userData["birthday"] as? String
                   var email = userData["email"] as? String

                    print(result) 
                }
        }  
    }   
    else
    {
        print(error.localizedDescription)
    }

}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
{
   print("user logged out")
}

在阅读按钮中我有这个代码:

<?xml version="1.0" encoding="utf-8" ?>
<date>
    <Disciplina nume="Disc1" cadru="Cadru1">
        <Student>
            <Nume>Student1</Nume>
            <Nota>9</Nota>
        </Student>
    </Disciplina> 
</date>

问题是我不知道如何从Student节点读取信息。你能救我吗?

1 个答案:

答案 0 :(得分:1)

如果您确实需要使用XmlReader类,可以在控制台应用程序中使用如下代码:

var nestedLevel = 0;
var studentNestedLevel = 0;

using (var reader = XmlTextReader.Create(@"Test.xml"))
{
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
                nestedLevel++;
                if (studentNestedLevel > 0)
                    Console.Write("{0}: ", reader.Name);
                if (reader.Name.ToLower() == "student")
                    studentNestedLevel = nestedLevel;
                break;

            case XmlNodeType.Text:
                if (studentNestedLevel > 0)
                    Console.WriteLine("{0}", reader.Value);
                break;

            case XmlNodeType.XmlDeclaration:
            case XmlNodeType.ProcessingInstruction:
                if (studentNestedLevel > 0)
                    Console.WriteLine("{0}: {1}", reader.Name, reader.Value);
                break;

            case XmlNodeType.Comment:
                break;

            case XmlNodeType.EndElement:
                nestedLevel--;
                if (reader.Name.ToLower() == "student")
                    studentNestedLevel = 0;
                break;
        }
    }
}