0

I have an svg file defined as below ,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<svg width="200mm" height="300mm"
    xmlns="http://www.w3.org/2000/svg" dominant-baseline="hanging">
    <text x="139.85mm" y="1.85mm" font-size="12pt">"studentName"</text>
    <text x="142.05mm" y="289.72mm" font-size="12pt">"studentAge"</text>
</svg>

I have written a program to replace values stored in "" such as "studentName" with values that are actually assigned in program runtime , however I am finding it hard to replace all such values at once as I cannot apply the && operator.

Here is my code so far , would really appreciate some help

    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student();

            student.Name = "Max";
            student.Age = "10";


            string file = File.ReadAllText(@"D:\Del\structure.svg");
            updatedDocu(file, student);

        }


        public static string updatedDocu(string intialDocument , Student studemt)
        {

            string updatedDoc = intialDocument.Replace("{studentName}", studemt.Name) && intialDocument.Replace("{studentAge}",studemt.Age);
            return updatedDoc;

        }
    }
}

public class Student
{
    public Student()
    {

    }

    public string Name{ get; set; }
    public string Age { get; set; }

}

  • Does this answer your question? [Replace Multiple String Elements in C#](https://stackoverflow.com/questions/1321331/replace-multiple-string-elements-in-c-sharp) – DeanOC May 28 '20 at 03:53
  • `&&` is a logical operator, not a string operator. You should be getting an error like `operator '&&' cannot be applied to operands of type 'string' and 'string'` – DeanOC May 28 '20 at 03:56
  • We do not recommend using string methods on XML files. There are lots of Net XML libraries that will do a better job than using String and Regex methods. – jdweng May 28 '20 at 09:52

4 Answers4

1

You need to replace

string updatedDoc = intialDocument.Replace("{studentName}", studemt.Name) && intialDocument.Replace("{studentAge}",studemt.Age);

with

string updatedDoc = intialDocument.Replace("\"studentName\"", studemt.Name).Replace("\"studentAge\"",studemt.Age);

it will work. Please correct spelling mistake of student object (studemt) to student

vinothvs
  • 1,224
  • 1
  • 10
  • 17
1

I would suggest using stringbuilder, for example:

public static string updatedDocu(string intialDocument, Student student)
        {
            return new StringBuilder(initialDocument)
                       .Replace("{studentName}", student.Name)
                       .Replace("{studentAge}", student.Age)
                       .ToString();
        }
TestBench
  • 26
  • 4
0

Try following xml linq :

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = doc.Root.GetDefaultNamespace();

            Dictionary<string, XElement> dict = doc.Descendants(ns + "text")
                .GroupBy(x => ((string)x).Replace("\"",""), y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            XElement studentName = dict["studentName"];
            studentName.SetValue("John");

            XElement studentAge = dict["studentAge"];
            studentAge.SetValue(20);



        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

I don't know whether it's the approach you need, but you could automate the process with the use of reflection to get the property from the object based on the property name mentioned in XML. Please, note that I don't check whether a property exists on an object (in your case - Student).

var xmlstr = @"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
    <svg width='200mm' height='300mm'
        xmlns='http://www.w3.org/2000/svg' dominant-baseline='hanging'>
        <text x='139.85mm' y='1.85mm' font-size='12pt'>{studentName}</text>
        <text x='142.05mm' y='289.72mm' font-size='12pt'>{studentAge}</text>
    </svg>";
XNamespace ns = "http://www.w3.org/2000/svg";
var xml = XElement.Parse(xmlstr);
var student = new Student { Name = "Steve", Age = "30" };
var type = student.GetType();
foreach(var text in xml.Elements(ns + "text"))
{
    // Get the name of the property we need to find in Student
    var prop_name = Regex.Match(text.Value, "[A-Z][a-z]+(?=})").Value;
    // Get the property on Student object
    var prop = type.GetProperty(prop_name);
    // Get the value of the propery on Student object
    var prop_val = prop.GetValue(student) as string;
    // Replace value of <text> element in XML 
    text.Value = prop_val;
}
JohnyL
  • 6,894
  • 3
  • 22
  • 41
  • Never use Regex on XML. Regex is for regular expressions and Xml is not regular. You modified the OPs xml file to use curly brackets instead of double quotes and replace the OPs double quotes with single quotes. – jdweng May 28 '20 at 10:34
  • @jdweng 1) Yes, I replaced `"` with `{}` because OP uses them in his code. I could change them to `"`, of course. 2) Didn't understand your concern about using Regex. I retrieve the *value* of `` tag. Regex has no processing of XML. The idea is to automate the process. If OP decides to add more properties, the code will handle this case, and no modifications would need. – JohnyL May 28 '20 at 10:49
  • That is exactly why it is not recommended to use string methods nor Regex on HTML and XML files. Look at my answer for correct way of editing XML. – jdweng May 28 '20 at 11:17
  • @jdweng *and replace the OPs double quotes with single quotes* And what? – JohnyL May 28 '20 at 12:50