2011-12-05 28 views
6

Tôi đang tìm trên Internet cách giữ vận chuyển trở lại từ dữ liệu XML nhưng tôi không thể tìm thấy câu trả lời, vì vậy tôi ở đây :)Làm thế nào giữ vận chuyển trở lại từ phân tích cú pháp XML

Mục tiêu là viết một tệp nội dung của dữ liệu XML. Vì vậy, nếu giá trị của nút chứa một số dữ liệu "\ r \ n", thì phần mềm cần phải ghi chúng vào tệp để tạo dòng mới, nhưng nó không viết, ngay cả với không gian: bảo toàn.

Đây là lớp học thử nghiệm của tôi:

XElement xRootNode = new XElement("DOCS"); 
XElement xData = null; 

//XNamespace ns = XNamespace.Xml; 
//XAttribute spacePreserve = new XAttribute(ns+"space", "preserve"); 
//xRootNode.Add(spacePreserve); 

xData = new XElement("DOC"); 
xData.Add(new XAttribute("FILENAME", "c:\\titi\\prout.txt")); 
xData.Add(new XAttribute("MODE", "APPEND")); 
xData.Add("Hi my name is Baptiste\r\nI'm a lazy boy"); 

xRootNode.Add(xData); 

bool result = Tools.writeToFile(xRootNode.ToString()); 

Và đây là quá trình lớp học của tôi:

try 
{ 
    XElement xRootNode = XElement.Parse(xmlInputFiles); 
    String filename = xRootNode.Element(xNodeDoc).Attribute(xAttributeFilename).Value.ToString(); 
    Boolean mode = false; 
    try 
    { 
     mode = xRootNode.Element(xNodeDoc).Attribute(xWriteMode).Value.ToString().ToUpper().Equals(xWriteModeAppend); 
    } 
    catch (Exception e) 
    { 
     mode = false; 
    } 

    String value = xRootNode.Element(xNodeDoc).Value; 
    StreamWriter destFile = new StreamWriter(filename, mode, System.Text.Encoding.Unicode); 

    destFile.Write(value); 
    destFile.Close(); 

    return true; 
} 
catch (Exception e) 
{ 
    return false; 
} 

Có ai có một ý tưởng?

+0

bạn nên đặt dữ liệu đó vào vì XML sẽ cố gắng để phân tích chúng. – Burimi

+0

Làm thế nào để làm điều đó? – BaptX

+0

Công việc của một trình phân tích cú pháp xml để chuẩn hóa các nguồn cấp dữ liệu dòng khác nhau thành \ n. Vì vậy, đây là hành vi mặc định. – Totonga

Trả lời

3

Nếu bạn muốn giữ cr lf trong phần tử hoặc thuộc tính nội dung khi lưu một XDocument hoặc XElement bạn có thể làm điều đó bằng cách sử dụng một số XmlWriterSettings, cụ thể là NewLineHandling để Entitize:

 string fileName = "XmlOuputTest1.xml"; 

     string attValue = "Line1.\r\nLine2."; 
     string elementValue = "Line1.\r\nLine2.\r\nLine3."; 

     XmlWriterSettings xws = new XmlWriterSettings(); 
     xws.NewLineHandling = NewLineHandling.Entitize; 

     XDocument doc = new XDocument(new XElement("root", 
             new XAttribute("test", attValue), 
             elementValue)); 

     using (XmlWriter xw = XmlWriter.Create(fileName, xws)) 
     { 
      doc.Save(xw); 
     } 

     doc = XDocument.Load(fileName); 
     Console.WriteLine("att value: {0}; element value: {1}.", 
          attValue == doc.Root.Attribute("test").Value, 
          elementValue == doc.Root.Value); 

Trong ví dụ rằng giá trị được bảo quản trong chuyến đi vòng tiết kiệm và tải khi đầu ra của mẫu là "att value: True; giá trị phần tử: True".

+0

Điều đó rất hữu ích, cảm ơn! –

0

Đó là một liên kết hữu ích mà tôi đã tìm thấy để phân tích cú pháp chuỗi Xml có trả về màu sắc, dòng nguồn cấp dữ liệu trong đó.

howto-correctly-parse-using-xelementparse-for-strings-that-contain-newline-character-in

Nó có thể giúp những người đang phân tích chuỗi Xml.

Đối với những người không thể bị làm phiền để nhấp vào nó nói sử dụng một XmlTextReader thay

XmlTextReader xtr = new XmlTextReader(new StringReader(xml)); 
    XElement items = XElement.Load(xtr); 
    foreach (string desc in items.Elements("Item").Select(i => (string)i.Attribute("Description"))) 
    { 
     Console.WriteLine("|{0}|", desc); 
    } 
Các vấn đề liên quan