【问题标题】:Update xml innerText with similar tag (element) names使用相似的标签(元素)名称更新 xml innerText
【发布时间】:2014-07-01 13:23:23
【问题描述】:

我的问题是我的 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>

【问题讨论】:

    标签: c# xml innertext


    【解决方案1】:

    问题 即使您从 XPath 查询中在 XmlNodeList 中返回了多个联系人,您也只是在更新第一个节点。

    如果您确定您的 XPath 查询应该返回一个联系人或不返回联系人,那么您应该使用 SelectSingleNode

    这里是fiddle

    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";
    

    更新:(解决方案 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";
    

    【讨论】:

    • 感谢您的回复。抱歉 Hassan Nisar,我的代码可能不够清楚。我现在已经编辑了原始帖子。如何在 XmlNodeList 中再次搜索?有可能吗?
    • XmlNodeList 表示节点的有序集合。您可以使用循环遍历 XmlNodeList。如果您确定将返回一个结果,则可以使用SelectSingleNode
    • 我已经尝试过了,但 XmlNodeList 似乎有问题,它实际上并没有在从 XPath 查询中获得的 XmlNode 中进行搜索。其他值 ex:photo,firstname,etc.. 正在更新为 XPath 搜索的 XmlNode。但它只是在它之外更新的电子邮件;在第一个联系人元素标签中。
    • 电子邮件节点发生了什么?仍然只有第一个节点得到更新?你能更新你的问题吗?
    • 我已经更新了原帖,添加了xml的代码。你能看看吗?谢谢
    猜你喜欢
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多