Вопрос

When I tryto remove text in an xml file, ie. rsquo, I get a 'System.OutOfMemoryException' exception error.

Any advice? - Its written in vb

   <Person>
   <Name>&rsquo; John /&rsquo; </Name>
   <age> 24 </age>
   <DOB>
      <year> 1990 </year>
      <month> 03 </month>
      <date> 23 </date>
  </DOB>
</Person>

   Dim myStreamReaderL1 As StreamReader
   Dim myStream As StreamWriter
   Dim mystr As String

   myStreamReaderL1 = System.IO.File.OpenText(Server.MapPath( filepath_Label.Text))
   myStr = myStreamReaderL1.ReadToEnd() 
   myStreamReaderL1.Close()

   If mystr.Contains("&rsquo;") Then
     mystr = mystr.Replace("&rsquo;", "")
     count += count + 1
    End If

    myStream = System.IO.File.CreateText(Server.MapPath("Valedate/" & DateTime.Now.ToString("yyyy-MM-dd") & "/new" & TextBox_filename.Text))
    myStream.WriteLine(myStr)
    myStream.Close()
Это было полезно?

Решение

Here's your mistake:

myStr = myStreamReaderL1.ReadToEnd() 

You only need ReadToEnd() if you need repeated nonsequential access to widely-separated positions in your file. If you are doing that you should not be using a StreamReader. You should not use ReadToEnd() to perform sequential access, like replacing a string on every line. Do something like this:

Using sr = System.IO.File.OpenText(Server.MapPath(filepath_Label.Text))
Using sw = System.IO.File.CreateText(Server.MapPath("Valedate/" & DateTime.Now.ToString("yyyy-MM-dd") & "/new" & TextBox_filename.Text))
    Dim l As String
    l = sr.ReadLine
    Do While (Not l Is Nothing)
        If l.Contains("&rsquo;") Then
            l = l.Replace("&rsquo;", "")
        End If

        sw.WriteLine(l)
        l = sr.ReadLine
    End While
End Using
End Using

Другие советы

The character sequence &rsquo; is the HTML Entity (named) for the Unicode Character 'RIGHT SINGLE QUOTATION MARK' (U+2019).

The entity &rsquo; isn't actually allowed in XML without first declaring it:

<!DOCTYPE section [ 
  <!ENTITY rsquo '&#x2019;'> 
]>

When it comes to working with XML files, you generally shouldn't do so as plain text. Instead, you should choose a specialized library designed for working with XML. Microsoft lists a number of approaches in its article XML Processing Options. Under the heading .NET Framework Options, Microsoft says for LINQ To XML, "Use this option if you're writing new code."

With that in mind, I've written a short program in C# to demonstrate a possible approach for you. (I'm hoping you can translate the concepts to VB.) Below, you'll find the expected output from the program following by the program itself.

Expected Output

<Persons>
  <Person>
    <Name> John / </Name>
    <age> 24 </age>
    <DOB>
      <year> 1990 </year>
      <month> 03 </month>
      <date> 23 </date>
    </DOB>
  </Person>
  <Person>
    <Name> Jane / </Name>
    <age> 21 </age>
    <DOB>
      <year> 1993 </year>
      <month> 04 </month>
      <date> 25 </date>
    </DOB>
  </Person>
</Persons>

Example Program

using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

class LinqToXmlDemo
{
    static public void Main(string[] args)
    {
        XDocument document = XDocument.Parse(GetXml());

        // Define a LINQ to XML query to return an enumeration of the
        // `Name` elements. (Those are the elements whose values you
        // wish to edit.)
        var query =
            from name in document.Root.Elements("Person").Elements("Name")
            select name;

        // The character that you want to remove.
        string s = Convert.ToChar(0x2019).ToString();

        // Iterate through each of the `Name` elements returned by the
        // query and remove the character of interest.
        foreach (var name in query)
        {
            if (name != null)
            {
                name.Value = ((string)name).Replace(s, String.Empty);
            }
        }

        // Output the edited document.
        Console.WriteLine(document.Root.ToString());
    }

    static string GetXml()
    {
        return
            @"<?xml version='1.0' encoding='UTF-8' ?> 
              <!DOCTYPE section [ 
                <!ENTITY rsquo '&#x2019;'> 
              ]>
              <Persons>
                <Person>
                  <Name>&rsquo; John /&rsquo; </Name>
                  <age> 24 </age>
                  <DOB>
                     <year> 1990 </year>
                     <month> 03 </month>
                     <date> 23 </date>
                  </DOB>
                </Person>
                <Person>
                  <Name>&rsquo; Jane /&rsquo; </Name>
                  <age> 21 </age>
                  <DOB>
                     <year> 1993 </year>
                     <month> 04 </month>
                     <date> 25 </date>
                  </DOB>
                </Person>
              </Persons>";
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top