2013年11月20日 星期三

[C#] 將現有節點從一個文件複製到另一個文件;ImportNode

在C#中,建立xml要先建立XmlDocument,在裡面再建立XmlElement
若想要將其中的節點,複製到其他XmlDocument可不能直接放
需使用ImportNode複製到其他的XmlDocument

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace qntest1
{
    static class Program
    {
        /// 
        /// 應用程式的主要進入點。
        /// 
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            xml();
        }

        static void xml()
        {
            //建立時間放在檔名
            DateTime dt = DateTime.Now;
            string time = string.Format("{0:yyyyMMddHHmmssffff}", dt);

            //新增第一個xml檔
            XmlDocument doc = new XmlDocument();
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(xmlDeclaration);
            XmlElement rootElement = doc.CreateElement("Company1");
            doc.AppendChild(rootElement);

            //新增資料
            XmlElement childElement = doc.CreateElement("Employee");
            XmlAttribute childAtrbt = doc.CreateAttribute("Department");
            childAtrbt.Value = "研發部";
            childElement.Attributes.Append(childAtrbt);
            rootElement.AppendChild(childElement);

            doc.Save(@"C:\Users\user\Desktop\test\" + "test_" + time + ".xml");

            //////////////////////////////////////////////////////////////////////////////////////

            //新增第二個xml檔
            XmlDocument doc2 = new XmlDocument();
            XmlDeclaration xmlDeclaration2 = doc2.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc2.AppendChild(xmlDeclaration2);

            XmlElement rootElement2 = doc2.CreateElement("Company2");
            doc2.AppendChild(rootElement2);

            //將第一個xml檔最後的節點複製到第二個xml檔
            XmlNode newNode = doc2.ImportNode(doc.DocumentElement.LastChild, true);
            doc2.DocumentElement.AppendChild(newNode);

            doc2.Save(@"C:\Users\user\Desktop\test\" + "test.copy_" + time + ".xml");
        }
    }
}


test_201311201933485572.xml
   
- 
       
  

test.copy_201311201933485572.xml
   
- 
       
  

[git][cygwin] git config

Git的設定有三種層級,分別是
1. /etc/gitconfig:系統,適用所有使用者及所有repositories;git config --system [command]
2. ~/.gitconfig:使用者,適用於該使用者;git config --global [command]
3. repositories/.git/config:repositories,適用於該repositories;git config [command]

一般用第二個,給自己的帳戶使用

/home/user/.gitonfig
[user]
        name = user
        email = user@email.com
[color]
        diff = auto
        status = auto
        branch = auto
        ui = auto 
[core]
        editor = nano
        autocrlf = false

[user] name, email:name為使用git commit會儲存的name及email
[color] 如果有需要可以填
[core] editor:編輯器若沒有指定會使用系統預設的,通常是vi或vim
[core] autocrlf:在不同作業系統(ex: Windows, Linux)換行字元會不同,若設定true,它會自動切換換行字元(crlf, lf)。
在Windows我使用cygwin,作為使用git的工具,為了避免換行字元會因為不同作業系統而改變,因此我設為false,依照codebase原有的換行字元


※ ref1:http://git-scm.com/book/en/Getting-Started-First-Time-Git-Setup
※ ref2:http://huan-lin.blogspot.com/2011/05/git-coreautocrlf.html

[Linux] 在目錄之下搜尋文件檔名及文件內關鍵字

在目錄之下,搜尋文件檔名
$ find -iname "[file_name]"
EX: $ fine iname "*.txt"

在目錄之下,搜尋文件關鍵字
$ grep -rin "[keyword]" .
EX: $ grep -rin "HelloWord()" .

2013年11月13日 星期三

[Linux] Ubuntu 12.04的下載來源

若是預設的下載來源會讓平時apt-get install下載時找不到檔案
那麼可以將下載來源換成以下
修改/etc/apt/source.list
#deb cdrom:[Ubuntu 12.04.1 LTS _Precise Pangolin_ - Release amd64 (20120823.1)]/ dists/precise/main/binary-i386/

#deb cdrom:[Ubuntu 12.04.1 LTS _Precise Pangolin_ - Release amd64 (20120823.1)]/ dists/precise/restricted/binary-i386/
#deb cdrom:[Ubuntu 12.04.1 LTS _Precise Pangolin_ - Release amd64 (20120823.1)]/ precise main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://tw.archive.ubuntu.com/ubuntu/ precise main restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ precise main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://tw.archive.ubuntu.com/ubuntu/ precise-updates main restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ precise-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://tw.archive.ubuntu.com/ubuntu/ precise universe
deb-src http://tw.archive.ubuntu.com/ubuntu/ precise universe
deb http://tw.archive.ubuntu.com/ubuntu/ precise-updates universe
deb-src http://tw.archive.ubuntu.com/ubuntu/ precise-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://tw.archive.ubuntu.com/ubuntu/ precise multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu/ precise multiverse
deb http://tw.archive.ubuntu.com/ubuntu/ precise-updates multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu/ precise-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://tw.archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu precise-security main restricted
deb-src http://security.ubuntu.com/ubuntu precise-security main restricted
deb http://security.ubuntu.com/ubuntu precise-security universe
deb-src http://security.ubuntu.com/ubuntu precise-security universe
deb http://security.ubuntu.com/ubuntu precise-security multiverse
deb-src http://security.ubuntu.com/ubuntu precise-security multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu precise partner
# deb-src http://archive.canonical.com/ubuntu precise partner

## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
deb http://extras.ubuntu.com/ubuntu precise main
deb-src http://extras.ubuntu.com/ubuntu precise main


2013年11月1日 星期五

[Android][Example] 用SoundPool控制左右聲道

如果需要控制左右聲道的音量,SoundPool有提供這個功能
SoundPool的優點是可以同時撥好幾個音檔
但卻對音檔的大小有限制
因此只適合撥較短的音檔

以下範例UI會有左右聲道的播放及停止按鈕





activity_main.xml


    

        

        

    

        

        



MainActivity.java
package com.qn.leftrightsound;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private String TAG = "leftrightsound";
    private SoundPool soundPool;
    private Button button_left_play, button_left_pause, button_right_play, button_right_pause;
    private int music_left, music_right;
    private int music_left_temp, music_right_temp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        music_left = soundPool.load(this, R.raw.left_sound, 1);
        music_right = soundPool.load(this, R.raw.right_sound, 1);
        // 音樂檔案放在res/raw底下,預設沒有raw資料夾,要自己建

        button_left_play = (Button) findViewById(R.id.button_left_play);
        button_left_pause = (Button) findViewById(R.id.button_left_pause);
        button_right_play = (Button) findViewById(R.id.button_right_play);
        button_right_pause = (Button) findViewById(R.id.button_right_pause);

        button_left_play.setOnClickListener(ButtonListner);
        button_left_pause.setOnClickListener(ButtonListner);
        button_right_play.setOnClickListener(ButtonListner);
        button_right_pause.setOnClickListener(ButtonListner);
    }

    private Button.OnClickListener ButtonListner = new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button_left_play:
                Log.i(TAG, "press left play");
                music_left_temp = soundPool.play(music_left, 1, 1, 0, 0, 1);
                // 由於每次播放的id會不同,為考慮到按下暫停是要暫停剛剛播放的,因此將每次播放的id存起來
                // play()
                // 第一參數為音檔id
                // 第二.三參數為左右聲道的音量,range = 0.0 to 1.0
                // 第四參數為優先權,若不需設定可都設為0,0為最小優先權
                // 第五參數為loop,0 = no loop, -1 = loop forever
                // 第六參數為速度,1.0 = normal playback, range 0.5 to 2.0
                break;
            case R.id.button_left_pause:
                Log.i(TAG, "press left pause");
                soundPool.stop(music_left_temp);
                 break;
            case R.id.button_right_play:
                Log.i(TAG, "press right play");
                music_right_temp = soundPool.play(music_right, 0, 1, 0, 0, 1);
                break;
            case R.id.button_right_pause:
                Log.i(TAG, "press right pause");
                soundPool.stop(music_right_temp);
                break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        this.soundPool.release();
         super.onPause();
    }

}