adding Hebrew content to XML file - Powershel



{sorry for my bad english}


ok my problem is a bit complicated. I have a config.xml file and I need to add some Hebrew content in to it so far so good, first time adding the info goes well but the next time i add info all the hebrew Signs become gibberish If the last information that was inserted was in Hebrew it displays properly (only the lest info displays properly)


here is my code



function Select-Folder($message='Select a folder', $path = 0)
{
$object = New-Object -comObject Shell.Application
$folder = $object.BrowseForFolder(0, $message, 0, $path)
if ($folder -ne $null)
{
button "Enter Folders" "Series Name" "Target Folder" $folder.self.Path;
}
}


function button ($title,$mailbx, $WF, $TF)
{
###################Load Assembly for creating form & button######

[void][System.Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.VisualBasic")

#####Define the form size & placement

$form = New-Object "System.Windows.Forms.Form";
$form.Width = 500;
$form.Height = 150;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

##############Define text label1
$textLabel1 = New-Object "System.Windows.Forms.Label";
$textLabel1.Left = 25;
$textLabel1.Top = 15;

$textLabel1.Text = $mailbx;

##############Define text label2

$textLabel2 = New-Object "System.Windows.Forms.Label";
$textLabel2.Left = 25;
$textLabel2.Top = 50;

$textLabel2.Text = $WF;


############Define text box1 for input
$textBox1 = New-Object "System.Windows.Forms.TextBox";
$textBox1.Left = 145;
$textBox1.Top = 10;
$textBox1.width = 250;

############Define text box2 for input

$textBox2 = New-Object "System.Windows.Forms.TextBox";
$textBox2.Left = 145;
$textBox2.Top = 50;
$textBox2.width = 250;


#############Define default values for the input boxes
$defaultValue = ""
$textBox1.Text = $defaultValue;
$textBox2.Text = $TF;

#############define OK button
$button = New-Object "System.Windows.Forms.Button";
$button.Left = 190;
$button.Top = 85;
$button.Width = 100;
$button.Text = "Ok";


#############define Cancel button
$button2 = New-Object "System.Windows.Forms.Button";
$button2.Left = 295;
$button2.Top = 85;
$button2.Width = 100;
$button2.Text = "Cancel";

############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$form.Close();};
$button.Add_Click($eventHandler);
$button2.Add_Click({$form.Close()});

#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($button2);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$ret = $form.ShowDialog();

#################return values

return $textBox1.Text, $textBox2.Text
}



# Set the File Name
$scriptDir = Split-Path -parent $MyInvocation.MyCommand.Definition
$cfgName = "conf.xml"
$filePath = $scriptDir + "\" + $cfgName

if(Test-Path $filePath)
{
$fileContent = [xml](get-content ($filePath))
if(!($fileContent -eq $null))
{
$return = Select-Folder
$Title = $return[0]
$Dest = $return[1]
foreach($xmlItem in $fileContent.tvAutoMover.tv_item)
{
$TitleExist = $xmlItem.file_name.ToLower().CompareTo($Title.ToLower())
$DestExist = $xmlItem.destination.ToLower().CompareTo($Dest.ToLower())
if($TitleExist -eq 0 -or $DestExist -eq 0)
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup($Title + ": Already Exist!",0,"Error",0x1)
return
}
}
[xml]$ValueList = Get-Content $filePath

# Copy Object
[Object]$CopyValue = $ValueList.tvAutoMover.tv_item | Where-Object {$_.file_name -eq "LEVE_THIS_LINE"}
$NweValue = $CopyValue.Clone()

# Add Series to new Object
$NweValue.file_name = $Title
$NweValue.destination = $Dest

# Add Series to XML Object
$ValueList.tvAutoMover.AppendChild($NweValue)

#Save to XML object to XML file
$ValueList.Save($filePath)
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup($Title + ": Was added Secssefully.",0,"OK",0x1)
}
else
{
Remove-Item $filePath -Force
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Config file Empty!`nRebuilding file...",0,"Error",0x1)

# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$Null)

# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"

# Write the XML Decleration
$xmlWriter.WriteStartDocument()

# Write Root Element
$xmlWriter.WriteStartElement("tvAutoMover")

# Write the Document
$xmlWriter.WriteStartElement("tv_item")
$xmlWriter.WriteElementString("file_name","LEVE_THIS_LINE")
$xmlWriter.WriteElementString("destination","LEVE_THIS_LINE")
$xmlWriter.WriteEndElement

# Write Close Tag for Root Element
$xmlWriter.WriteEndElement
# End the XML Document
$xmlWriter.WriteEndDocument()

# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
}
}
else
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Config file Missing!`nRebuilding file...",0,"Error",0x1)
# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$Null)

# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"

# Write the XML Decleration
$xmlWriter.WriteStartDocument()

# Write Root Element
$xmlWriter.WriteStartElement("tvAutoMover")

# Write the Document
$xmlWriter.WriteStartElement("tv_item")
$xmlWriter.WriteElementString("file_name","LEVE_THIS_LINE")
$xmlWriter.WriteElementString("destination","LEVE_THIS_LINE")
$xmlWriter.WriteEndElement

# Write Close Tag for Root Element
$xmlWriter.WriteEndElement
# End the XML Document
$xmlWriter.WriteEndDocument()

# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
}


I think the relevant part is



[xml]$ValueList = Get-Content $filePath
# Copy Object from Banana
[Object]$CopyValue = $ValueList.tvAutoMover.tv_item | Where-Object {$_.file_name -eq $Match}
$NweValue = $CopyValue.Clone()
# Add Fruit to new Object
$NweValue.file_name = $Title
$NweValue.destination = $Dest
# Add Fruit to XML Object
$ValueList.tvAutoMover.AppendChild($NweValue)
#Save to XML object ot XML file
$ValueList.Save($filePath)

No comments:

Post a Comment