Console program to Ctrl+H XML

Discuss anything programming related.
Post Reply
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Console program to Ctrl+H XML

Post by bumlove »

I have a need for a simple console program and I don't know what to do.

The desire is it add POI entries to an XML (database.xml) (about 100, all the same for every entry) before the <\song> is closed off, so the simple way is NP++, edit find "</Song>" and replace with "My_lines/r</Song>"
Which is the quickest solution for a one off. But the end users are more useless than me.

I've followed some stuff and made, database.xsd and database.cs but I've only been blindly following instruction.

This is an example of the xml

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<VirtualDJ_Database Version="8.0.0">
 <Song FilePath="F:\music\Jump Around To Insane Kolomeika (DJ Dodgy-Style Mash-Up).wav" FileSize="35132840" Flag="8192">
  <Tags Author="DJ Dodgy-Style " Title="Jump Around To Insane Kolomeika" Flag="2" />
  <Infos SongLength="199.142540" FirstSeen="1394322480" FirstPlay="1395607860" LastPlay="1415213613" PlayCount="4" Bitrate="1411" Color="16517356" Cover="32" />
  <Comment>mash up insane in t membrain</Comment>
  <Scan Version="800" Bpm="0.545828" AltBpm="0.818662" Volume="0.705265" Key="A" />
  <Poi Pos="0.743039" Type="automix" Point="fadeStart" />
  <Poi Pos="4.992336" Type="beatgrid" />
  <Poi Pos="4.996644" Type="automix" Point="cutStart" />
 </Song>
 <Song FilePath="F:\music\RRR023-A_Hajkan_Bajkan_-_Elephant_Step_Stantekk_rmx.flac" FileSize="26720302">
  <Tags Flag="1" />
  <Infos SongLength="216.118367" FirstSeen="1401483615" FirstPlay="1409758608" LastPlay="1415208116" PlayCount="2" Bitrate="989" Cover="34" />
  <Scan Version="800" Bpm="0.600000" AltBpm="0.600000" Volume="0.896348" Key="C" Flag="1" />
  <Poi Pos="2.700590" Type="beatgrid" />
  <Poi Type="automix" Point="fadeStart" />
  <Poi Pos="214.087982" Type="automix" Point="fadeEnd" />
 </Song>
</VirtualDJ_Database>
Advice I've been given
generate your model class with xsd.exe, input xml is vdj database.
http://blogs.msdn.com/b/yojoshi/archive ... d-exe.aspx

- deserialize the xml databases into objects. i.e. like this:

Code: Select all

foreach (string pathToDatabase in _databaseFilePaths)
                {
                    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                    xmlReaderSettings.CheckCharacters = false;
                   
                    XmlSerializer ser = new XmlSerializer(typeof(VirtualDJ_Database));
                    VirtualDJ_Database database;

                    using (XmlReader reader = XmlReader.Create(pathToDatabase, xmlReaderSettings))
                    {
                        database = (VirtualDJ_Database)ser.Deserialize(reader);
                        databaseCollection.Add(pathToDatabase, database);
                    }
                }
- do what ever you want with the db-objects

- serialize the objects back into xml

- done : )
Any help at all would be great, thanks.
User avatar
JacksonCougar
Huurcat
Posts: 2460
Joined: Thu Dec 06, 2007 11:30 pm
Location: Somewhere in Canada

Re: Console program to Ctrl+H XML

Post by JacksonCougar »

Well... if you have a schema in C# you can load it as an object in C# and edit it's properties and then serialize it back into xml.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Console program to Ctrl+H XML

Post by bumlove »

Ay'up jackson

That's the problem, I've done all the gay C# hello world calculator shit and did a bit of entity editing, but nothing from scratch and nothing unguided.
It'd be a bit stupid to spend several weeks struggling with something someone can knock up in 20 minutes. Especially since it's really only raw text editing.

I kind of feel treating it as txt based Ctrl+H program would be good enough.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Console program to Ctrl+H XML

Post by bumlove »

Sorry for bleating like the prize noob,information overload kicks in quite quickly with me & code, I try save my memory for building regs and cable calculations.

So anyway I found this pre-made from M$ so I can read the xml, It's not reading all info but I'm working with a different sample.xml to M$

any pointers how to, if text== </Song> then delete the line and add 2 lines of <!-- xml comments --> and re add </Song> (it can't be much can it?)

Code: Select all

using System;
using System.Xml;

namespace Trial
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        static void Main(string[] args)
        {
            XmlTextReader reader = new XmlTextReader("C:\\Sample\\database.xml");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        Console.Write("<" + reader.Name);
                        Console.WriteLine(">");
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        Console.WriteLine(reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
            }
            Console.ReadLine();
        }
    }
}
I know my method is terrible programming but it doesn't have to be smart or account for anything, it's just a temporary bodge until the VirtualDJ programmers take over.
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Console program to Ctrl+H XML

Post by bumlove »

I've been digging around, and approaching it as a purely text find and replace exercise is the simplest, I can follow what's happening here just I don't know how to put it together.
So please anyone, this must be like ABC for you guys.

Find&Replace text

Code: Select all

StreamReader reading = File.OpenText("test.txt");
string str;
while ((str = reading.ReadLine())!=null)
{
      if (str.Contains("some text"))
      {
          StreamWriter write = new StreamWriter("test.txt");
      }
}
Now i have found the text however i have no idea on how to replace the text in the file with my text.
Read all file content. Make a replacement with String.Replace. Write content back to file.

Code: Select all

string text = File.ReadAllText("test.txt");
text = text.Replace("some text", "new value");
File.WriteAllText("test.txt", text);
User avatar
Prey
Posts: 129
Joined: Sat Dec 29, 2007 5:06 pm
Location: UK

Re: Console program to Ctrl+H XML

Post by Prey »

Code: Select all

File.WriteAllText(@"C:\Sample\database.xml", 
                File.ReadAllText(@"C:\Sample\database.xml")
                .Replace("</song>", "My_lines\r\n</Song>"));
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Console program to Ctrl+H XML

Post by bumlove »

Cool, I thought it would be a 2 second job for seasoned pro's
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Console program to Ctrl+H XML

Post by bumlove »

Ok I think that's solved it, Thanks a million Prey it might not seem like much compared to your usual coding challanges but it's quite a leap forward.

Code: Select all

using System.IO;

namespace Trial
{
    /// <summary>
    /// Simple text find & replace
    /// </summary>
    /// 
    class Class1
    {
        static void Main()
        {
            File.WriteAllText(@"C:\Sample\database.xml",
                            File.ReadAllText(@"C:\Sample\database.xml")
                            .Replace("</Song>", " Line_1\n  Line_2\n  Line_3\n </Song>"));
        }
    }
}
I've got a very similar project, but a bit trickier, a different xml,
I'd like to get to the customButtons entry (in the xml it is one line) and count the "," symbols up to the close of customButtons, but ignoring ones in square brackets "[]" I'd then want to add more "," symbols to take the count up to 99, and finally add my own string.

My intuition is telling me get to there I need to be and read a char at a time, with IFs & BUTs to deal with bracketed ","s (I can think like coding but it's never syntactically correct)

Are we still in the realm of easy, or is it studying time for me?
(I promise this is my final request, if we're still in easy land)

Code: Select all

	<skins>
		<coloredWaveforms>monochrome</coloredWaveforms>
		<beatCounterRange>16</beatCounterRange>
		<skin />
		<maximized>1</maximized>
		<customButtons>, , , , , , , [poop=toast, pee=beans](2), poo=cheese</customButtons>
	</skins>
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

Re: Console program to Ctrl+H XML

Post by Zaid »

This is what I got from what you said. Probably not what you want, but I was bored.
Attachments
How many bums.7z
(6.32 KiB) Downloaded 304 times
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Console program to Ctrl+H XML

Post by bumlove »

Thanks for this Zaid, I can only just follow it but that's how we learn

The program throws an exception after opening the xml when I click load
*Edit my fault for not being precise, the hierarchy is /settings/skins/customButtons, so after sorting that out it didn't crash* :oops:

After trying it and adding a random string, my customButtons entry looks like this.
So it looks like "<customButtons>" is being wrote in also,

Code: Select all

    <customButtons>dg=&lt;/customButtons&gt;, , , , , , , [poop=toast, pee=beans](2),poo=cheese , rtgrg,</customButtons>
The main thing is I can see the counting logic, so I can try port that into a console app.
(It has to be a console app, I intend to add 100 custom buttons in batch and some will be lengthy VDJ script)

Could you add the logic so when your press save it adds "," so there are 99 in total, then adds the string
User avatar
Zaid
Posts: 250
Joined: Sun Jan 09, 2011 2:07 am

Re: Console program to Ctrl+H XML

Post by Zaid »

Just changed the for loop when you save?

Code: Select all

 for (int i = 0; i < 99; i++)
            {
                if(BumListBox.Items.Count > i)
                ButtonNode.InnerText += BumListBox.Items[i].ToString() + ',';
                else
                    ButtonNode.InnerText += ',';
            }
User avatar
bumlove
Posts: 524
Joined: Tue Dec 11, 2007 8:10 am
Location: England I'm not British, I'm English

Re: Console program to Ctrl+H XML

Post by bumlove »

all sorted thanks very much to Zaid
Post Reply