Created
November 17, 2011 04:36
-
-
Save seraphy/1372370 to your computer and use it in GitHub Desktop.
ASP.NETで、Linq to XMLによるデータの読み込みとJSONへの変換、とデシリアライズ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[WebMethod] | |
public static string GetComboInfo( | |
string dep, string area, string block, string dmNo, string fieldName) | |
{ | |
// パラメータの診断 | |
System.Diagnostics.Debug.Print( | |
String.Format("dep:{0}, area:{1}, block:{2}, cdmNo:{3}, changed:{4}", | |
dep, area, block, dmNo, fieldName | |
)); | |
// XMLの読み込み | |
HttpContext context = HttpContext.Current; | |
string xmlFile = context.Server.MapPath("~/App_Data/dmChoose.xml"); | |
XDocument doc = XDocument.Load(xmlFile); | |
// DmNO指定の場合、検索する | |
bool dmNoMatched = false; | |
var ps = from c in doc.Root.Descendants("Person") | |
where c.Attribute("id").Value == dmNo | |
select c; | |
if (ps.Count() > 0) | |
{ | |
dmNoMatched = true; | |
if (fieldName == "dmNo") | |
{ | |
dep = ps.First().Ancestors("Department").First().Attribute("id").Value; | |
area = ps.First().Ancestors("Area").First().Attribute("id").Value; | |
block = ps.First().Ancestors("Block").First().Attribute("id").Value; | |
} | |
} | |
// 部門リスト | |
var deps = from c in doc.Root.Descendants("Department") | |
select new { | |
id = c.Attribute("id").Value, | |
name = c.Attribute("name").Value | |
}; | |
// エリアリスト | |
var areas = from c in ( | |
from d in doc.Root.Descendants("Department") | |
where d.Attribute("id").Value == dep | |
select d | |
).Descendants("Area") | |
select new | |
{ | |
id = c.Attribute("id").Value, | |
name = c.Attribute("name").Value | |
}; | |
// ブロック要素リスト | |
var blkNodes = from c in ( | |
from a in ( | |
from d in doc.Root.Descendants("Department") | |
where d.Attribute("id").Value == dep | |
select d | |
).Descendants("Area") | |
where a.Attribute("id").Value == area | |
select a | |
).Descendants("Block") | |
select c; | |
// ブロックリスト | |
var blocks = from c in blkNodes | |
select new | |
{ | |
id = c.Attribute("id").Value, | |
name = c.Attribute("name").Value | |
}; | |
// 氏名リスト | |
var persons = from c in ( | |
from b in blkNodes | |
where b.Attribute("id").Value == block | |
select b | |
).Descendants("Person") | |
select new | |
{ | |
id = c.Attribute("id").Value, | |
name = c.Attribute("name").Value | |
}; | |
var results = new | |
{ | |
dep = deps, | |
area = areas, | |
block = blocks, | |
person = persons, | |
choose = new | |
{ | |
dep, | |
area, | |
block, | |
dmNo, | |
dmNoMatched | |
} | |
}; | |
// JSONに変換 | |
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); | |
string json = serializer.Serialize(results); | |
System.Diagnostics.Debug.Print("JSON=" + json); | |
#if DEBUG | |
// ※ このJSONをデシリアライズすると、 | |
// 結果はリストまたはディクショナリとなる. | |
object o = serializer.DeserializeObject(json); | |
System.Diagnostics.Debug.Print("deserialize=" + o); | |
var ret = o as Dictionary<string, object>; | |
foreach (string name in ret.Keys) | |
{ | |
var val = ret[name]; | |
System.Diagnostics.Debug.Print("*** name=" + name + "/val=" + val); | |
var lst = val as object[]; | |
if (lst != null) | |
{ | |
foreach (Dictionary<string, object> item in lst) | |
{ | |
foreach (string key in item.Keys) | |
{ | |
System.Diagnostics.Debug.Print( | |
"key:" + key + "=" + item[key]); | |
} | |
} | |
} | |
else | |
{ | |
var dict = val as Dictionary<string, object>; | |
if (dict != null) | |
{ | |
foreach (var keyValuePair in dict) | |
{ | |
System.Diagnostics.Debug.Print( | |
"key:" + keyValuePair.Key + | |
"=" + keyValuePair.Value); | |
} | |
} | |
} | |
} | |
#endif | |
return json; | |
} | |
/* ~/App_Data/dmChoose.xml にあるデータ | |
<?xml version="1.0" encoding="utf-8" ?> | |
<DmChoose> | |
<Departments> | |
<Department id="1" name="アルファ"> | |
<Areas> | |
<Area id="1" name="Aエリア"> | |
<Blocks> | |
<Block id="1" name="第1ブロック"> | |
<Persons> | |
<Person id="99999" name="一郎"/> | |
<Person id="99998" name="二郎"/> | |
</Persons> | |
</Block> | |
<Block id="2" name="第2ブロック"> | |
<Persons> | |
<Person id="99997" name="三郎"/> | |
</Persons> | |
</Block> | |
</Blocks> | |
</Area> | |
<Area id="2" name="Bエリア"> | |
<Blocks> | |
<Block id="3" name="第3ブロック"> | |
</Block> | |
<Block id="4" name="第4ブロック"> | |
<Persons> | |
<Person id="10000" name="A郎"/> | |
</Persons> | |
</Block> | |
<Block id="5" name="第5ブロック"> | |
<Persons> | |
<Person id="10001" name="B郎"/> | |
</Persons> | |
</Block> | |
</Blocks> | |
</Area> | |
<Area id="3" name="Cエリア"> | |
<Blocks> | |
<Block id="6" name="第6ブロック"> | |
<Persons> | |
<Person id="10002" name="C郎"/> | |
</Persons> | |
</Block> | |
</Blocks> | |
</Area> | |
</Areas> | |
</Department> | |
<Department id="2" name="ベータ"> | |
<Areas> | |
<Area id="4" name="Dエリア"> | |
<Blocks> | |
<Block id="7" name="第7ブロック"> | |
<Persons> | |
<Person id="10003" name="D郎"/> | |
<Person id="10004" name="E郎"/> | |
</Persons> | |
</Block> | |
</Blocks> | |
</Area> | |
<Area id="5" name="Eエリア"> | |
<Blocks> | |
<Block id="8" name="第8ブロック"> | |
<Persons> | |
<Person id="10005" name="F郎"/> | |
</Persons> | |
</Block> | |
<Block id="9" name="第9ブロック"> | |
</Block> | |
</Blocks> | |
</Area> | |
</Areas> | |
</Department> | |
<Department id="3" name="ガンマ"> | |
<Areas> | |
<Area id="6" name="Fエリア"> | |
<Blocks> | |
<Block id="10" name="第10ブロック"> | |
<Persons> | |
<Person id="10006" name="F郎"/> | |
<Person id="10007" name="G郎"/> | |
</Persons> | |
</Block> | |
<Block id="11" name="第11ブロック"> | |
<Persons> | |
<Person id="10008" name="H郎"/> | |
<Person id="10009" name="I郎"/> | |
<Person id="10010" name="J郎"/> | |
</Persons> | |
</Block> | |
</Blocks> | |
</Area> | |
<Area id="7" name="Gエリア"> | |
<Blocks> | |
<Block id="12" name="第12ブロック"> | |
</Block> | |
<Block id="13" name="第13ブロック"> | |
</Block> | |
<Block id="14" name="第14ブロック"> | |
</Block> | |
</Blocks> | |
</Area> | |
<Area id="8" name="Hエリア"> | |
<Blocks> | |
<Block id="15" name="第15ブロック"> | |
</Block> | |
<Block id="16" name="第16ブロック"> | |
</Block> | |
</Blocks> | |
</Area> | |
<Area id="9" name="Iエリア"> | |
<Blocks> | |
<Block id="17" name="第17ブロック"> | |
<Persons> | |
<Person id="10011" name="K郎"/> | |
</Persons> | |
</Block> | |
<Block id="18" name="第18ブロック"> | |
<Persons> | |
<Person id="10012" name="L郎"/> | |
<Person id="10013" name="M郎"/> | |
</Persons> | |
</Block> | |
<Block id="19" name="第19ブロック"> | |
<Persons> | |
<Person id="10014" name="N郎"/> | |
<Person id="10015" name="O郎"/> | |
<Person id="10016" name="P郎"/> | |
</Persons> | |
</Block> | |
</Blocks> | |
</Area> | |
</Areas> | |
</Department> | |
</Departments> | |
</DmChoose> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment