2013-07-31 76 views
0

我正在使用ASP.Net中的XML編輯器,它允許用戶打開XML文件,然後編輯並保存該XML文件。除了當用戶試圖保存文件時,所有東西都可以工作,它可以將文件從其加載的上下文中保存,而不是新編輯的版本。從屬性contenteditable ='true'的標記中獲取當前編輯的文本

FileUpload控件現在只能在IE中使用,但是我打算在從其他Web應用程序傳遞目錄之後才能完成此功能,所以稍後將不再需要它。

CSS:

#lineNum 
{ 
min-width:30px; 
float:left; 
color:#F8F8F2; 
border-right:1px solid #F8F8F2; 
margin-right:10px; 
/* 
UNSELECTABLE 
*/ 
-moz-user-select: -moz-none; 
-khtml-user-select: none; 
-webkit-user-select: none; 
-ms-user-select: none; 
user-select: none; 
} 
#editor 
{ 
width:100%; 
} 
.tagName 
{ 
color:#f92772; 
} 
.tagVal 
{ 
color:#fd9620; 
} 
.attrName 
{ 
color:#bce9fd; 
} 
.attrVal 
{ 
color:#bc95ff; 
} 
.comment 
{ 
color:#75715e !important; 
} 
body 
{ 
background-color:#272822; 
font-size: 12px; 
font-family: Menlo; 
color: orange; 
} 
a 
{ 
color:#bce9fd; 
} 

C#:

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace XML 
{ 
    public partial class index : System.Web.UI.Page 
    { 
     public static bool comment; 

     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void menu_MenuItemClick(object sender, MenuEventArgs e) 
     { 
      fileUp.Visible = false; 
      switch (e.Item.Text) 
      { 
       case "Open": 
        fileUp.Visible = true; 
        break; 
       case "Save": 
        saveXml(fileContents.Text); 
        break; 
      } 
     } 

     protected void openFile_Click(object sender, EventArgs e) 
     { 
      if (getFile.HasFile) 
      { 
       lblFileLoc.Text = getFile.PostedFile.FileName; 
       /*lblFileLoc.Text = Server.MapPath(@".\tmp\temp.xml"); 
       getFile.SaveAs(lblFileLoc.Text);*/ 
       lblFileName.Text = getFile.FileName; 
       lblStat.Text = "Current File: <a href='" + lblFileLoc.Text + "'>" + lblFileName.Text + "</a>"; 
       fileUp.Visible = false; 
       fileContents.Text = "<pre contenteditable='true'>"; 
       lineNum.InnerText = "\n\n"; 
       string[] xml = File.ReadAllLines(lblFileLoc.Text); 
       comment = false; 
       for (int i = 1; i <= xml.Length; i++) 
       { 
        lineNum.InnerText += i + "\n"; 
        fileContents.Text += highlightLine(xml[i - 1]); 
       } 
       fileContents.Text += "</pre>"; 
      } 
     } 

     public void saveXml(string xml) 
     { 
      xml = xml.Replace("<XMP class='comment'>", ""); 
      xml = xml.Replace("<XMP class='attrName'>", ""); 
      xml = xml.Replace("<XMP class='attrVal'>", ""); 
      xml = xml.Replace("<XMP class='tagName'>", ""); 
      xml = xml.Replace("<XMP class='tagVal'>", ""); 
      xml = xml.Replace("</XMP>", ""); 
      xml = xml.Replace("<pre contenteditable='true'>", ""); 
      xml = xml.Replace("</pre>", ""); 
      if (xml.Trim().Length > 0) 
       File.WriteAllText(lblFileLoc.Text, xml); 
     } 

     protected string highlightLine(string line) 
     { 
      string hlString = ""; 
      int lastIndex = 0; 
      while (line.IndexOf('<', lastIndex) > -1) 
      { 
       int openStart = (line.Contains("<!--")) ? line.IndexOf("<!--", lastIndex) : line.IndexOf('<', lastIndex); 
       int openEnd = (line.Contains("-->")) ? line.IndexOf("-->", openStart) : line.IndexOf('>', openStart); 
       hlString += line.Substring(0, openStart); 

       if (line.Contains("<!--") || line.Contains("-->")) 
       { 
        if (line.Contains("<!--") && line.Contains("-->")) 
         hlString += "<XMP class='comment'>" + line.Substring(openStart, (openEnd + 3 - openStart)); 
        if (line.Contains("<!--") && !line.Contains("-->")) 
        { 
         comment = true; 
         hlString += "<XMP class='comment'>" + line.Substring(openStart, (line.Length - openStart)); 
         break; 
        } 
        if (!line.Contains("<!--") && line.Contains("-->")) 
         hlString += line.Substring(openStart, (line.Length - openStart)); 
        if (line.Contains("-->")) 
        { 
         hlString += "</XMP>"; 
         comment = false; 
         if (line.Substring((line.Length-3), 3) == "-->") 
          break; 
         else 
          lastIndex = openEnd; 
        } 
       } 
       else if (comment) 
       { 
        hlString += line.Substring(openStart, (line.Length - openStart)); 
        lastIndex = openEnd; 
       } 
       if (!comment) 
       { 
        if (line.IndexOf('=', lastIndex) > -1) 
        { 
         string[] words = line.Substring(openStart, (openEnd + 1 - openStart)).Split(' '); 
         foreach (string word in words) 
         { 
          if (word.Contains('=')) 
          { 
           string[] attr = word.Split('='); 
           hlString += "<XMP class='attrName'>" + attr[0] + "</XMP>=<XMP class='attrVal'>"; 

           if (attr[1].Contains("/>")) 
            hlString += attr[1].Replace("/>", "") + "</XMP><XMP class='tagName'>/></XMP>"; 
           else if (attr[1].Contains(">")) 
            hlString += attr[1].Replace(">", "") + "</XMP><XMP class='tagName'>></XMP>"; 
           else if (attr[1].Contains("?>")) 
            hlString += attr[1].Replace("?>", "") + "</XMP><XMP class='tagName'>?></XMP>"; 
           else 
            hlString += attr[1] + "</XMP> "; 
          } 
          else 
           hlString += "<XMP class='tagName'>" + word + "</XMP> "; 
         } 
        } 
        else 
        { 
         hlString += "<XMP class='tagName'>" + line.Substring(openStart, (openEnd + 1 - openStart)) + "</XMP>"; 
        } 
        lastIndex = openEnd; 
        if (!line.Substring(openStart, (lastIndex + 1 - openStart)).Contains('/') && line.IndexOf('<', lastIndex) > -1) 
        { 
         int closeStart = line.IndexOf('<', lastIndex); 
         int closeEnd = line.IndexOf('>', lastIndex + 1); 
         if (line.Substring(closeStart, (closeEnd + 1 - closeStart)).Contains('/')) 
         { 
          hlString += "<XMP class='tagVal'>" + line.Substring((lastIndex + 1), (closeStart - lastIndex - 1)) + "</XMP>"; 
          hlString += "<XMP class='tagName'>" + line.Substring(closeStart, (closeEnd + 1 - closeStart)) + "</XMP>"; 
          lastIndex = closeEnd; 
         } 
        } 
       } 
      } 
      return hlString + "\n"; 
     } 
    } 
} 

ASP:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="XML.index" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <link href="styles/main.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
    <form id="form" runat="server"> 
     <div> 
      <asp:Menu ID="menu" runat="server" Orientation="Horizontal" 
       onmenuitemclick="menu_MenuItemClick" BackColor="#272822" ForeColor="orange"> 
       <DynamicMenuItemStyle BackColor="#272822" ForeColor="orange" /> 
       <Items> 
        <asp:MenuItem Text="File"> 
         <asp:MenuItem Text="Open" Value="Open"></asp:MenuItem> 
         <asp:MenuItem Text="Save" Value="Save"></asp:MenuItem> 
        </asp:MenuItem> 
       </Items> 
      </asp:Menu> 
      <asp:Label ID="lblStat" runat="server" /> 
      <asp:Label ID="lblFileName" runat="server" Visible="False" /> 
      <asp:Label ID="lblFileLoc" runat="server" Visible="False" /> 
      <br /> 
      <div id="fileUp" runat="server" visible="False"> 
       <asp:FileUpload ID="getFile" runat="server"></asp:FileUpload> 
       <asp:Button ID="openFile" Text="Open" runat="server" onclick="openFile_Click"/> 
      </div> 
      <div id="editor"> 
       <pre id="lineNum" runat="server"></pre> 
       <pre> 
        <asp:Literal ID="fileContents" runat="server"></asp:Literal> 
       </pre> 
      </div> 
     </div> 
    </form> 
</body> 
</html> 
+0

也許你的保存問題是文件權限。 –

+0

你想粘貼什麼鏈接?我可以編輯你的帖子並粘貼給你。 – Dozer789

+0

@ Dozer789鏈接是爲[CSS](http://pastebin.com/RfURVGWD) – Divide100

回答

0

我用JS解決了這個問題。

我得到了「fileContents」的價值,並把它變成所謂的「rawXML」一個隱藏字段:

document.getElementById("rawXML").value = document.getElementById("fileContents").innerText;  

然後我用了「rawXML」的價值,並寫道,該文件。儘管如此,這是一個非常不穩定的解決方法,讓我知道是否有人找到更好的方法。

相關問題