无法使用XElement加载带有重音字符的文件

时间:2014-04-05 09:03:38

标签: c#

我尝试以下代码,但它无法正常运行,请任意。

string file = @"C:\Program.xml"; 
XElement root = XElement.Parse(File.ReadAllText(file).Replace("\"", "'"));

XML文件的示例:

<?xml version="1.0" encoding="UTF-8"?>
<session xmlns="http://winscp.net/schema/session/1.0" name="test" start="2014-04-04T15:54:09.728Z">
  <upload>
    <filename value="D:\ftp\test2.TXT" />
    <destination value="/in/test2.TXT" />
    <result success="true" />
  </upload>
  <touch>
    <filename value="/in/test2.TXT" />
    <modification value="2014-03-27T12:45:20.000Z" />
    <result success="false" />
  </touch>
</session>

我需要使用XElement进行进一步治疗

1 个答案:

答案 0 :(得分:2)

我认为你有点困惑

Xdocument xdoc=Xdocument.Load(filepath);

这就是你需要的所有东西,你可以在没有任何问题的情况下使用xml移动

xdoc.root.elements("nameElement").Attributes("nameAttribute").Value

等等。

这很简单:)

这里有一个简单的例子:在vbnet中然后在c#中。假设您有一个简单的网页,其中包含一个带有事件的按钮

 Protected Sub btnGetValues_Click(sender As Object, e As EventArgs) Handles btnGetValues.Click

    Dim xdoc As XDocument = XDocument.Load(Server.MapPath("~/data.xml"))
    Dim ns As XNamespace = "http://winscp.net/schema/session/1.0"
    Dim Sb As New StringBuilder
    Try
        'iterate within xmlelement where assume with this code that "session" is root
        'and descendant are upload and its child and touch with its childs
        For Each el In (From a In xdoc.Root.Descendants(ns + "upload") Select a)
            For Each subelement In el.Descendants
                Response.Write("<b>" & subelement.Name.ToString & "</b><ul>")
                If subelement.HasAttributes Then
                    For Each att In subelement.Attributes
                        Response.Write("<li>" & att.Name.ToString & ":" & att.Value.ToString & "</li>")
                    Next
                End If
                Response.Write("</ul>")
            Next
        Next

    Catch ex As Exception
        Response.Write(ex.Message)
    End Try


End Sub

C#版本:

protected void btnGetValues_Click(object sender, EventArgs e)
{
XDocument xdoc = XDocument.Load(Server.MapPath("~/data.xml"));
XNamespace ns = "http://winscp.net/schema/session/1.0";
StringBuilder Sb = new StringBuilder();
try {
    //iterate within xmlelement where assume with this code that "session" is root
    //and descendant are upload and its child and touch with its childs
    foreach (object el_loopVariable in (from a in xdoc.Root.Descendants(ns + "upload")a)) {
        el = el_loopVariable;
        foreach (object subelement_loopVariable in el.Descendants) {
            subelement = subelement_loopVariable;
            Response.Write("<b>" + subelement.Name.ToString + "</b><ul>");
            if (subelement.HasAttributes) {
                foreach (object att_loopVariable in subelement.Attributes) {
                    att = att_loopVariable;
                    Response.Write("<li>" + att.Name.ToString + ":" + att.Value.ToString + "</li>");
                }
            }
            Response.Write("</ul>");
        }
    }

} catch (Exception ex) {
    Response.Write(ex.Message);
}


}

这是作为response.write:

的页面结果

{http://winscp.net/schema/session/1.0}文件名

  • 值:d:\ ftp的\的test2.txt
{ http://winscp.net/schema/session/1.0}目的地
  • 值:/in/test2.TXT
{http://winscp.net/schema/session/1.0}结果
  • 成功:真