Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to convert from string {X=-24,Y=10} which generated by Point.ToString(); to Point again?

I save the string value in xml file in save mode and I want read it back to point again in read mode.

We're going to need more context. You probably don't want to do that, and can instead just copy the Point , rather than converting it to a string. Daniel Mann Apr 28, 2012 at 19:07 I convert it to string to save it in xml file and I need to get it back from xml file to point kartal Apr 28, 2012 at 19:09 Use the System.Drawing.PointConverter class. Both when you serialize and deserialize. Or just don't bother at all since xml serialization can do this automatically. Hans Passant Apr 28, 2012 at 19:29
var myStringWhichCantBeChanged="{X=-24,Y=10}";
var g=Regex.Replace(myStringWhichCantBeChanged,@"[\{\}a-zA-Z=]", "").Split(',');
Point pointResult = new Point(
                  int.Parse (g[0]),
                  int.Parse( g[1]));
                Just for the record, starting from .NET 3.0 there is a 'System.Drawing.Point.Parse()' function
– AndresRohrAtlasInformatik
                Dec 16, 2020 at 9:04

System.Drawing.Point doesn't define a Parse method at all - you will need to write your own that can take this format and return a Point structure.

System.Windows.Point does have a Parse method and may be more suitable for your needs.

However, since you are outputting to XML, non of this should be needed. You should be serializing and deserializng the object graph, which would take care of this automatically without you needing to worry about parsing and formatting.

I can't because the string structure for system.windows.point is different that system.drawing.point in system.windows.point = "10,10" in system.drawing.point = {X=10,Y=10} – kartal Apr 28, 2012 at 19:27

Hans Passant had the right solution: Don't use Point.ToString(), which gives you that crazy, un-reusable string (MSDN calls it "human-readable"). Use the PointConverter class instead.

To generate the string:

Dim myPoint As New Point(0, 0)
Dim pointConverter As System.ComponentModel.TypeConverter = _
    System.ComponentModel.TypeDescriptor.GetConverter(GetType(Point))
Dim pointAsString As String = pointConverter.ConvertToString(myPoint)

And to interpret the above string:

Dim pointConverter As System.ComponentModel.TypeConverter = _
    System.ComponentModel.TypeDescriptor.GetConverter(GetType(Point))
Dim myNewPoint As New Point = pointConverter.ConvertFromString(pointAsString)

I needed this functionality today, myself, so I just coded it. Here is a very picky parser, using a "TryParse" approach. I don't like what I call 'lazy' parsing where "blah4,9anything" would get parsed as a point. And I don't like throwing errors. The 'TryParse' methods of data types are very robust to me. So here's my implementation for any to use. My only request is if you find a bug, please let me know! :)

public static bool TryParsePoint(string s, out System.Drawing.Point p)
{   p = new System.Drawing.Point();
    string s1 = "{X=";
    string s2 = ",Y=";
    string s3 = "}";
    int x1 = s.IndexOf(s1, StringComparison.OrdinalIgnoreCase);
    int x2 = s.IndexOf(s2, StringComparison.OrdinalIgnoreCase);
    int x3 = s.IndexOf(s3, StringComparison.OrdinalIgnoreCase);
    if (x1 < 0 || x1 >= x2 || x2 >= x3) { return false; }
    s1 = s.Substring(x1 + s1.Length, x2 - x1 - s1.Length);
    s2 = s.Substring(x2 + s2.Length, x3 - x2 - s2.Length); int i = 0;
    if (Int32.TryParse(s1, out i) == false) { return false; } p.X = i;
    if (Int32.TryParse(s2, out i) == false) { return false; } p.Y = i;
    return true;
} // public static bool TryParsePoint(string s, out System.Drawing.Point p)

Note you may also want to remove or change the public or static modifier(s) of the method. But I used that method in the Program class, so mine needed to be public static. Suit yourself tho.

i like this too, but i second Oded's point that for XML, one should use the automatic serialization and deserialization. but when one needs a stand-alone method, i like Edward's code because it needs no other namespaces or helper functions. it stands on its own. +1 – Shawn Kovac Sep 9, 2014 at 20:06

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.