2012-02-20 6 views
-3

XML 문서를 읽으려고합니다. 이제 "/ word/link1"에서 읽는다면 정보를 호출 할 수 있지만 더 깊은 "buttons/word/link1"로 가면 바로 InnerText를 읽지 못합니다.XML 파일에서 읽기 C#

public void rxml() 
    { 
     XmlTextReader read = new XmlTextReader("C:\\myXmFile.xml"); 
     XmlDocument doc = new XmlDocument(); 
     doc.Load(read); 
     switch (P) 
     { 
      case "word": 
       label11.Text = "Microsoft Word Documents"; 

       l1 = doc.SelectSingleNode("buttons/word/link1").InnerText = "hello"; 
       p1 = doc.SelectSingleNode("buttons/word/path1").InnerText; 
       l2 = doc.SelectSingleNode("buttons/word/link2").InnerText = linkLabel2.Text; 
       p2 = doc.SelectSingleNode("buttons/word/path2").InnerText; 
       l3 = doc.SelectSingleNode("buttons/word/link3").InnerText = linkLabel3.Text; 
       p3 = doc.SelectSingleNode("buttons/word/path3").InnerText; 
       l4 = doc.SelectSingleNode("buttons/word/link4").InnerText = linkLabel4.Text; 
       p4 = doc.SelectSingleNode("buttons/word/path4").InnerText; 
       l5 = doc.SelectSingleNode("buttons/word/link5").InnerText = linkLabel5.Text; 
       p5 = doc.SelectSingleNode("buttons/word/path5").InnerText; 
       l6 = doc.SelectSingleNode("buttons/word/link6").InnerText = linkLabel6.Text; 
       p6 = doc.SelectSingleNode("buttons/word/path6").InnerText; 
       l7 = doc.SelectSingleNode("buttons/word/link7").InnerText = linkLabel7.Text; 
       p7 = doc.SelectSingleNode("buttons/word/path7").InnerText; 
       l8 = doc.SelectSingleNode("buttons/word/link8").InnerText = linkLabel8.Text; 
       p8 = doc.SelectSingleNode("buttons/word/path8").InnerText; 
       l9 = doc.SelectSingleNode("buttons/word/link9").InnerText = linkLabel9.Text; 
       p9 = doc.SelectSingleNode("buttons/word/path9").InnerText; 
       l10 = doc.SelectSingleNode("buttons/word/link10").InnerText = linkLabel10.Text; 
       p10 = doc.SelectSingleNode("buttons/word/path10").InnerText; 
       read.Close(); 
       break; 
      case "excel": 
       label11.Text = "Microsoft Excel Documents"; 
       l1 = doc.SelectSingleNode("buttons/excel/link1").InnerText = linkLabel1.Text; 
       p1 = doc.SelectSingleNode("buttons/excel/path1").InnerText; 
       l2 = doc.SelectSingleNode("buttons/excel/link2").InnerText = linkLabel2.Text; 
       p2 = doc.SelectSingleNode("buttons/excel/path2").InnerText; 
       l3 = doc.SelectSingleNode("buttons/excel/link3").InnerText = linkLabel3.Text; 
       p3 = doc.SelectSingleNode("buttons/excel/path3").InnerText; 
       l4 = doc.SelectSingleNode("buttons/excel/link4").InnerText = linkLabel4.Text; 
       p4 = doc.SelectSingleNode("buttons/excel/path4").InnerText; 
       l5 = doc.SelectSingleNode("buttons/excel/link5").InnerText = linkLabel5.Text; 
       p5 = doc.SelectSingleNode("buttons/excel/path5").InnerText; 
       l6 = doc.SelectSingleNode("buttons/excel/link6").InnerText = linkLabel6.Text; 
       p6 = doc.SelectSingleNode("buttons/excel/path6").InnerText; 
       l7 = doc.SelectSingleNode("buttons/excel/link7").InnerText = linkLabel7.Text; 
       p7 = doc.SelectSingleNode("buttons/excel/path7").InnerText; 
       l8 = doc.SelectSingleNode("buttons/excel/link8").InnerText = linkLabel8.Text; 
       p8 = doc.SelectSingleNode("buttons/excel/path8").InnerText; 
       l9 = doc.SelectSingleNode("buttons/excel/link9").InnerText = linkLabel9.Text; 
       p9 = doc.SelectSingleNode("buttons/excel/path9").InnerText; 
       l10 = doc.SelectSingleNode("buttons/excel/link10").InnerText = linkLabel10.Text; 
       p10 = doc.SelectSingleNode("buttons/excel/path10").InnerText; 
       read.Close(); 
       break; 
      case "adobe": 
       label11.Text = "Adobe PDF Documents"; 
       l1 = doc.SelectSingleNode("buttons/adobe/link1").InnerText = linkLabel1.Text; 
       p1 = doc.SelectSingleNode("buttons/adobe/path1").InnerText; 
       l2 = doc.SelectSingleNode("buttons/adobe/link2").InnerText = linkLabel2.Text; 
       p2 = doc.SelectSingleNode("buttons/adobe/path2").InnerText; 
       l3 = doc.SelectSingleNode("buttons/adobe/link3").InnerText = linkLabel3.Text; 
       p3 = doc.SelectSingleNode("buttons/adobe/path3").InnerText; 
       l4 = doc.SelectSingleNode("buttons/adobe/link4").InnerText = linkLabel4.Text; 
       p4 = doc.SelectSingleNode("buttons/adobe/path4").InnerText; 
       l5 = doc.SelectSingleNode("buttons/adobe/link5").InnerText = linkLabel5.Text; 
       p5 = doc.SelectSingleNode("buttons/adobe/path5").InnerText; 
       l6 = doc.SelectSingleNode("buttons/adobe/link6").InnerText = linkLabel6.Text; 
       p6 = doc.SelectSingleNode("buttons/adobe/path6").InnerText; 
       l7 = doc.SelectSingleNode("buttons/adobe/link7").InnerText = linkLabel7.Text; 
       p7 = doc.SelectSingleNode("buttons/adobe/path7").InnerText; 
       l8 = doc.SelectSingleNode("buttons/adobe/link8").InnerText = linkLabel8.Text; 
       p8 = doc.SelectSingleNode("buttons/adobe/path8").InnerText; 
       l9 = doc.SelectSingleNode("buttons/adobe/link9").InnerText = linkLabel9.Text; 
       p9 = doc.SelectSingleNode("buttons/adobe/path9").InnerText; 
       l10 = doc.SelectSingleNode("buttons/adobe/link10").InnerText = linkLabel10.Text; 
       p10 = doc.SelectSingleNode("buttons/adobe/path10").InnerText; 
       read.Close(); 
       break; 
      case "powerpoint": 
       label11.Text = "Microsoft Powerpoint Documents"; 
       l1 = doc.SelectSingleNode("buttons/powerpoint/link1").InnerText = linkLabel1.Text; 
       p1 = doc.SelectSingleNode("buttons/powerpoint/path1").InnerText; 
       l2 = doc.SelectSingleNode("buttons/powerpoint/link2").InnerText = linkLabel2.Text; 
       p2 = doc.SelectSingleNode("buttons/powerpoint/path2").InnerText; 
       l3 = doc.SelectSingleNode("buttons/powerpoint/link3").InnerText = linkLabel3.Text; 
       p3 = doc.SelectSingleNode("buttons/powerpoint/path3").InnerText; 
       l4 = doc.SelectSingleNode("buttons/powerpoint/link4").InnerText = linkLabel4.Text; 
       p4 = doc.SelectSingleNode("buttons/powerpoint/path4").InnerText; 
       l5 = doc.SelectSingleNode("buttons/powerpoint/link5").InnerText = linkLabel5.Text; 
       p5 = doc.SelectSingleNode("buttons/powerpoint/path5").InnerText; 
       l6 = doc.SelectSingleNode("buttons/powerpoint/link6").InnerText = linkLabel6.Text; 
       p6 = doc.SelectSingleNode("buttons/powerpoint/path6").InnerText; 
       l7 = doc.SelectSingleNode("buttons/powerpoint/link7").InnerText = linkLabel7.Text; 
       p7 = doc.SelectSingleNode("buttons/powerpoint/path7").InnerText; 
       l8 = doc.SelectSingleNode("buttons/powerpoint/link8").InnerText = linkLabel8.Text; 
       p8 = doc.SelectSingleNode("buttons/powerpoint/path8").InnerText; 
       l9 = doc.SelectSingleNode("buttons/powerpoint/link9").InnerText = linkLabel9.Text; 
       p9 = doc.SelectSingleNode("buttons/powerpoint/path9").InnerText; 
       l10 = doc.SelectSingleNode("buttons/powerpoint/link10").InnerText = linkLabel10.Text; 
       p10 = doc.SelectSingleNode("buttons/powerpoint/path10").InnerText; 
       read.Close(); 
       break; 
      case "publisher": 
       label11.Text = "Microsoft Publisher Documents"; 
       l1 = doc.SelectSingleNode("buttons/publisher/link1").InnerText = linkLabel1.Text; 
       p1 = doc.SelectSingleNode("buttons/publisher/path1").InnerText; 
       l2 = doc.SelectSingleNode("buttons/publisher/link2").InnerText = linkLabel1.Text; 
       p2 = doc.SelectSingleNode("buttons/publisher/path2").InnerText; 
       l3 = doc.SelectSingleNode("buttons/publisher/link3").InnerText = linkLabel1.Text; 
       p3 = doc.SelectSingleNode("buttons/publisher/path3").InnerText; 
       l4 = doc.SelectSingleNode("buttons/publisher/link4").InnerText = linkLabel1.Text; 
       p4 = doc.SelectSingleNode("buttons/publisher/path4").InnerText; 
       l5 = doc.SelectSingleNode("buttons/publisher/link5").InnerText = linkLabel1.Text; 
       p5 = doc.SelectSingleNode("buttons/publisher/path5").InnerText; 
       l6 = doc.SelectSingleNode("buttons/publisher/link6").InnerText = linkLabel1.Text; 
       p6 = doc.SelectSingleNode("buttons/publisher/path6").InnerText; 
       l7 = doc.SelectSingleNode("buttons/publisher/link7").InnerText = linkLabel1.Text; 
       p7 = doc.SelectSingleNode("buttons/publisher/path7").InnerText; 
       l8 = doc.SelectSingleNode("buttons/publisher/link8").InnerText = linkLabel1.Text; 
       p8 = doc.SelectSingleNode("buttons/publisher/path8").InnerText; 
       l9 = doc.SelectSingleNode("buttons/publisher/link9").InnerText = linkLabel1.Text; 
       p9 = doc.SelectSingleNode("buttons/publisher/path9").InnerText; 
       l10 = doc.SelectSingleNode("buttons/publisher/link10").InnerText = linkLabel1.Text; 
       p10 = doc.SelectSingleNode("buttons/publisher/path10").InnerText; 
       read.Close(); 
       break; 
      case "access": 
       label11.Text = "Microsoft Access Documents"; 
       l1 = doc.SelectSingleNode("buttons/access/link1").InnerText = linkLabel1.Text; 
       p1 = doc.SelectSingleNode("buttons/access/path1").InnerText; 
       l2 = doc.SelectSingleNode("buttons/access/link2").InnerText = linkLabel2.Text; 
       p2 = doc.SelectSingleNode("buttons/access/path2").InnerText; 
       l3 = doc.SelectSingleNode("buttons/access/link3").InnerText = linkLabel3.Text; 
       p3 = doc.SelectSingleNode("buttons/access/path3").InnerText; 
       l4 = doc.SelectSingleNode("buttons/access/link4").InnerText = linkLabel4.Text; 
       p4 = doc.SelectSingleNode("buttons/access/path4").InnerText; 
       l5 = doc.SelectSingleNode("buttons/access/link5").InnerText = linkLabel5.Text; 
       p5 = doc.SelectSingleNode("buttons/access/path5").InnerText; 
       l6 = doc.SelectSingleNode("buttons/access/link6").InnerText = linkLabel6.Text; 
       p6 = doc.SelectSingleNode("buttons/access/path6").InnerText; 
       l7 = doc.SelectSingleNode("buttons/access/link7").InnerText = linkLabel7.Text; 
       p7 = doc.SelectSingleNode("buttons/access/path7").InnerText; 
       l8 = doc.SelectSingleNode("buttons/access/link8").InnerText = linkLabel8.Text; 
       p8 = doc.SelectSingleNode("buttons/access/path8").InnerText; 
       l9 = doc.SelectSingleNode("buttons/access/link9").InnerText = linkLabel9.Text; 
       p9 = doc.SelectSingleNode("buttons/access/path9").InnerText; 
       l10 = doc.SelectSingleNode("buttons/access/link10").InnerText = linkLabel10.Text; 
       p10 = doc.SelectSingleNode("buttons/access/path10").InnerText; 
       read.Close(); 
       break; 

     } 

    } 

XML 파일 http://pastebin.com/i5vVt0GV

+2

XML 파일 제공 ... –

+1

@ 마샬 나는이 코드가 실패한 짧은 XML 파일을 게시하는 것이 좋습니다. – spender

+0

xml과 예외/문제점을 제공하십시오. rxml()에 대한 완전한 코드 : – bang

답변

1

데이터를 더 쉽게, 당신은 데이터를 accesing 클래스를 작성해야 액세스 할 수 있도록합니다. 문제를 여러 수준으로 나누어 전체를 이해하고 디버그하며 최신 상태로 유지하기가 쉽습니다. 나는 앞서 가서 이것을 this file of extensions을 사용하여 만든 예를 만들었습니다. 코드 여기

ButtonsXml buttons = new ButtonsXml("C:\\myXmFile.xml"); 
string link1 = buttons["word"][1].Link; 

것 : 당신이 경로 또는 링크에 값을 설정하려는 경우

public class ButtonsXml 
{ 
    XElement self; 
    Dictionary<string, DataCollection> list = new Dictionary<string, DataCollection>(); 

    public ButtonsXml(string file) 
    { 
     self = XElement.Load(file); 
    } 

    public DataCollection this[string name] 
    { 
     get 
     { 
      if (list.ContainsKey(name)) 
       return list[name]; 
      DataCollection c = new DataCollection(self.GetElement(name)); 
      list.Add(name, c); 
      return c; 
     } 
    } 

    public void Save(string file) 
    { 
     self.Save(file); 
    } 
} 

public class DataCollection 
{ 
    XElement self; 
    public Dictionary<int, Element> list = new Dictionary<int,Element>(); 

    public DataCollection(XElement self) 
    { 
     this.self = self; 
    } 

    public Element this[int index] 
    { 
     get 
     { 
      if (list.ContainsKey(index)) 
       return list[index]; 
      Element e = new Element(self, index); 
      list.Add(index, e); 
      return e; 
     } 
    } 
} 

public class Element 
{ 
    XElement parent; 
    int index; 

    public Element(XElement parent, int index) 
    { 
     this.parent = parent; 
     this.index = index; 
    } 

    public string Path 
    { 
     get { return parent.GetString("path" + index.ToString(), string.Empty); } 
     set { parent.Set("path" + index.ToString(), value, XElementConversions.ELEMENT); } 
    } 

    public string Link 
    { 
     get { return parent.GetString("link" + index.ToString(), string.Empty); } 
     set { parent.Set("link" + index.ToString(), value, XElementConversions.ELEMENT); } 
    } 
} 

, 내가

을했습니다

은 당신이 wordlink1을 얻을 것 다음을 사용하려면 추가 된 설정자 : 동일한 방식으로 설정됩니다. 그런 다음 파일에 변경 사항을 저장하려면 buttons.Save(file)을 호출해야합니다.

buttons["word"][1].Link = "new value";