2014-04-05 2 views
0

다음 코드를 시도했지만 올바르게 작동하지 않았습니다.XElement를 사용하여 악센트 부호가있는 문자로 파일을로드하지 못했습니다.

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

XML 파일의 예 : 난 당신이 조금 전부 그

Xdocument xdoc=Xdocument.Load(filepath); 

혼란이라고 생각

<?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> 

내가 더 치료

답변

2

에 대한 XElement를를 사용할 필요가 당신은 예를 들어 아무 문제없이 withn 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# 버전과 간단한 웹 페이지가 있다고 가정 :

{http://winscp.net/schema/session/1.0} 파일 이름 :

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를 같은 페이지에 결과는

  • 값 : D : \ ftp \ test2.TXT
{http://winscp.net/schema/session/1.0} 대상
  • 값 : /in/test2.TXT
{http://winscp.net/schema/session/1.0} 결과
  • 성공 : 사실

+0

0 : 네임 스페이스의 모든 노드가 가지고있는 원래의 문서 - 당신이 좋아하는 네임 스페이스를 사용하는 예를 보여 주어야한다 http://stackoverflow.com/questions/4985974/xelement-namespaces-howtoto –

관련 문제