0

私の問題は、以下に示すように、xml ファイルに最大 3 回出現する電子メール要素があることです。

<contact> 
<email>A</email> 
<email>B</email> 
<email>C</email> 
</contact> 

以下のコードで3つのタグを編集しようとしたところ、

xnl[0]["email"].InnerText = "D"; 
xnl[0]["email"].InnerText = "E"; 
xnl[0]["email"].InnerText = "F"; 

最初のメールのみを編集すると、xml 要素名が同じであるために上書きされる問題があります。

<contact> 
<email>F</email> 
<email>B</email> 
<email>C</email> 
</contact> 

最初のメール要素名を選択しようとxnl[0]["email"][0].InnerText = "D";しましたが、うまくいきませんでした。なにか提案を?

更新:(コード付き)

c# コード:

        public String updateContact(Int32 getPhoneNumber, Int32 getWorkNumber, Int32 getMobileNumber, String photo, String firstName, String lastName, String gender, String dateOfBirth, Int32 home, Int32 work, Int32 mobile, String email1, String email2, String email3)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("xmlFile/xml/myContactBook/src/myContactBook.xml");

        XmlNodeList xnl = doc.SelectNodes("/contactBook/contact/phone[home='" + getPhoneNumber + "']/parent::* | /contactBook/contact/phone[work='" + getWorkNumber + "']/parent::* | /contactBook/contact/phone[mobile='" + getMobileNumber + "']/parent::* ");

        if (xnl.Count != 0)
        {
            xnl[0]["photo"].InnerText = photo;
            xnl[0]["firstName"].InnerText = firstName;
            xnl[0]["lastName"].InnerText = lastName;
            xnl[0]["gender"].InnerText = gender;
            xnl[0]["dateOfBirth"].InnerText = dateOfBirth;
            xnl[0]["phone"]["home"].InnerText = home.ToString();
            xnl[0]["phone"]["work"].InnerText = work.ToString();
            xnl[0]["phone"]["mobile"].InnerText = mobile.ToString();
            xnl[0]["email"].InnerText = email1;
            xnl[0]["email"].InnerText = email2;
            xnl[0]["email"].InnerText = email3;

        }

        doc.Save("xmlFile/xml/myContactBook/src/myContactBook.xml");

        return "Updated";
    }

xml コード:

<contact>
<photo>profilepic/default.jpg</photo>
<firstName>Hender</firstName>
<lastName>Casio</lastName>
<gender>Female</gender>
<dateOfBirth>1985-04-23</dateOfBirth>
<phone>
  <home>4453278</home>
  <work>3451390</work>
  <mobile>54356635</mobile>
</phone>
<email>casio.hender@mail.com</email>
<email>heder@aol.com</email>
<email>hencasio@yahoo.com</email>
</contact>
4

1 に答える 1

1

問題XPath クエリから 複数の連絡先が返された場合でも、XmlNodeList最初のノードを更新しているだけです。

XPath クエリが連絡先を 1 つ返すか、まったく返さないことが確実な場合は、SelectSingleNodeを使用する必要があります。

ここにフィドルがあります。

XmlDocument doc = new XmlDocument();
doc.Load("xmlFile/xml/myContactBook/src/myContactBook.xml");

XmlNode xn = doc.SelectSingleNode("/contactBook/contact/phone[home='" + getPhoneNumber + "']/parent::* | /contactBook/contact/phone[work='" + getWorkNumber + "']/parent::* | /contactBook/contact/phone[mobile='" + getMobileNumber + "']/parent::* ");

if (xn != null)
{
     xn["photo"].InnerText = photo;
     xn["firstName"].InnerText = firstName;
     xn["lastName"].InnerText = lastName;
     xn["gender"].InnerText = gender;
     xn["dateOfBirth"].InnerText = dateOfBirth;
     xn["phone"]["home"].InnerText = home.ToString();
     xn["phone"]["work"].InnerText = work.ToString();
     xn["phone"]["mobile"].InnerText = mobile.ToString();

     XmlNodeList xnl = xn.SelectNodes("email");
     xnl[0].InnerText = email1;
     xnl[1].InnerText = email2;
     xnl[2].InnerText = email3;
}

doc.Save("xmlFile/xml/myContactBook/src/myContactBook.xml");
return "Updated";

UPDATE : (解決策 2 ) XPath クエリから複数の結果が返される場合:

XmlDocument doc = new XmlDocument();
doc.Load("xmlFile/xml/myContactBook/src/myContactBook.xml");

XmlNodeList xnl = doc.SelectNodes("/contactBook/contact/phone[home='" + getPhoneNumber + "']/parent::* | /contactBook/contact/phone[work='" + getWorkNumber + "']/parent::* | /contactBook/contact/phone[mobile='" + getMobileNumber + "']/parent::* ");

foreach(XmlNode xn in xnl)
{
     xn["photo"].InnerText = photo;
     xn["firstName"].InnerText = firstName;
     xn["lastName"].InnerText = lastName;
     xn["gender"].InnerText = gender;
     xn["dateOfBirth"].InnerText = dateOfBirth;
     xn["phone"]["home"].InnerText = home.ToString();
     xn["phone"]["work"].InnerText = work.ToString();
     xn["phone"]["mobile"].InnerText = mobile.ToString();

     XmlNodeList xnlElement = xn.SelectNodes("email");
     xnlElement[0].InnerText = email1;
     xnlElement[1].InnerText = email2;
     xnlElement[2].InnerText = email3;
}

doc.Save("xmlFile/xml/myContactBook/src/myContactBook.xml");
return "Updated";
于 2014-05-13T18:33:29.957 に答える