2

C#でXMLファイルにアクセスするには? そのxmlファイルのノード数を数える方法は? そのxmlファイルのすべてのノードにアクセスするにはどうすればよいですか?

2 つの xml ファイルがあり、そのうちの 1 つはこのコードを持つ dev.xml です。

<Devanagri_to_itrans>
  <mapping>
    <character>अ&lt;/character>
    <itrans>a</itrans>
  </mapping>
  ...
</Devanagri_to_itrans>

2 番目のファイルは、guj.xml です (構造は非常に似ています)。

<Gujrathi_to_itrans>
  <mapping>
     <character>અ&lt;/character>
     <itrans>a</itrans>
  <mapping>
  ...
</Gujrathi_to_itrans>

これを文字マッピングの 2 次元配列に変換する必要があります。

4

3 に答える 3

2

Linq to XML に関するこのチュートリアルを試してください - http://www.switchonthecode.com/tutorials/introduction-to-linq-simple-xml-parsing

この質問 -オブジェクトごとに完全な XML 構造を取得して、XDocument を反復処理する方法は? - いくつかの興味深いコードも提供します

于 2011-03-19T15:22:29.793 に答える
1

.net 3.5 以降を使用している場合は、への参照を設定して LINQ to XML を使用しますSystem.Xml.Linq。以下は、コンソール アプリのウィンドウに対する特定の xml ファイル内の要素の単純な数です。

string xml = @"<xml><a/><a/><a/></xml>";
XDocument doc = XDocument.Parse(xml);
Console.WriteLine((from a in doc.Descendants("a")
                   select a).Count());
于 2011-03-19T15:24:31.567 に答える
0

Since you've added more details I can now provide a better answer. Here is a functional xml parsing and joining console app that demonstrates what it is you're looking for (I think). To parse xml files rather than xml strings use the XDocument Load method rather than the displayed Parse method. Good luck,

            XDocument docA = XDocument.Parse(
@"<Devanagri_to_itrans>
  <mapping>
    <character>अ&lt;/character>
    <itrans>a</itrans>
  </mapping>
</Devanagri_to_itrans>");
            XDocument docB = XDocument.Parse(
@"<Gujrathi_to_itrans>
  <mapping>
     <character>અ&lt;/character>
     <itrans>a</itrans>
  </mapping>
</Gujrathi_to_itrans>");
            var devanagriKeys = (from d in docA.Descendants("mapping")
                                                  select new {
                                                      Key = d.Descendants("itrans").FirstOrDefault().Value,
                                                      Character = d.Descendants("character").FirstOrDefault().Value
                                                  }).ToArray();
            var gujrathiKeys = (from g in docB.Descendants("mapping")
                                                  select new {
                                                      Key = g.Descendants("itrans").FirstOrDefault().Value,
                                                      Character = g.Descendants("character").FirstOrDefault().Value
                                                  }).ToArray();
            var crossReference = (from d in devanagriKeys
                                  join g in gujrathiKeys on d.Key equals g.Key
                                  select new {
                                        d.Key,
                                        Devanagri = d.Character,
                                        Gujrathi = g.Character
                                    }).ToList();
            Console.WriteLine("Enter a key character to translate:");
            string searchKey = Console.ReadLine();
            var translation = crossReference.Where(cr => cr.Key == searchKey).FirstOrDefault();
            if (translation == null) 
                Console.WriteLine("No such key in the cross reference.");
            else
                Console.WriteLine("{0} is {1} in Devanagri and {2} in Gujrathi", 
                    translation.Key, translation.Devanagri, translation.Gujrathi);
            Console.ReadKey(true);

PER REQUEST FOR SESSION VARIABLE:

Anonymous types are only intended for use within a method. To place a list into a Session variable for use elsewhere create a real class of your own that contains the 3 desired properties and change the line of code above very matching this to the below. (The class name I chose was CrossReferenceTranslation.)

        Session["CrossReference"] = (from d in devanagriKeys
                              join g in gujrathiKeys on d.Key equals g.Key
                              select new CrossReferenceTranslation() {
                                    d.Key,
                                    Devanagri = d.Character,
                                    Gujrathi = g.Character
                                }).ToList();

...then, at some other point in time you can do this to get your session object list into a variable. Note the assumption that the variable could be null, which would happen whenever a session has timed out...

List<CrossReferenceTranslation>() crossReference = Session["CrossReference"] ?? 
   new List<CrossReferenceTranslation>();
于 2011-03-19T20:01:57.917 に答える