Skip to content

Instantly share code, notes, and snippets.

@kowill
Created September 22, 2015 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kowill/20d17e3a46b5f6c063fe to your computer and use it in GitHub Desktop.
Save kowill/20d17e3a46b5f6c063fe to your computer and use it in GitHub Desktop.
XmlReaderでDTD読み込み
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
namespace DtdSample
{
class Program
{
static void Main(string[] args)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"Resources\Test.xml");
//.NET Framework 4.5.2からはコメントアウト行の記述ではダメ
//using (var reader = XmlReader.Create(path, new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse }))
using (var reader = XmlReader.Create(path, new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse, XmlResolver = new XmlUrlResolver() }))
{
XDocument srcTree = XDocument.Load(reader);
var data = srcTree.Descendants("Hoge")
.Select(x =>
{
var item = new Hoge();
item.Category = int.Parse(x.Element("Category").Value);
item.Title = x.Element("Title").Value;
item.Body = x.Element("Body").Value;
return item;
})
.ToList();
}
}
}
class Hoge
{
public int Category { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
}
サブ1
てすとーーーーーーーーーーーーーーーーーー
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE Samples[
<!ELEMENT Samples (Hoge+)>
<!ELEMENT Hoge (Category,Title,Body)>
<!ELEMENT Category (#PCDATA)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Body (#PCDATA)>
<!ENTITY Sub1 SYSTEM "Body\Sub1.txt">
<!ENTITY Sub2 SYSTEM "Body\Sub2.txt">
<!ENTITY Sub3 SYSTEM "Body\Sub3.txt">
]>
<Samples>
<Hoge>
<Category>0</Category>
<Title>タイトル1</Title>
<Body>&Sub1;</Body>
</Hoge>
<Hoge>
<Category>1</Category>
<Title>タイトル2</Title>
<Body>&Sub1;</Body>
</Hoge>
<Hoge>
<Category>2</Category>
<Title>タイトル3</Title>
<Body>&Sub1;</Body>
</Hoge>
</Samples>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment