【问题标题】:Linq to XML work with repeatable nodes CRUD operationsLinq to XML 使用可重复节点 CRUD 操作
【发布时间】:2013-12-10 05:40:17
【问题描述】:

我有一个这样的 xml:

<Root>
 <MortgaginContracts>
   <ContractInfo>
       <PledgeType>3</PledgeType>
       <ContractType>0</ContractType>
       <ContractNum>123</ContractNum>
   </ContractInfo>
   <ContractInfo>
       <PledgeType>4</PledgeType>
       <ContractType>3</ContractType>
       <ContractNum>125</ContractNum>
   </ContractInfo>
   <ContractInfo>
       <PledgeType>3</PledgeType>
       <ContractType>0</ContractType>
       <ContractNum>123</ContractNum>           
   </ContractInfo>
 </MorgaginContracts>
</Root>

没有可重复节点的标识符。如何在 MVC 项目中为“ContractInfo”创建具有 CRUD 操作的存储库?而 ContractInfo 可能有您的可重复子节点。

有什么想法吗? xmlnode分层多怎么解决?

【问题讨论】:

    标签: asp.net-mvc linq-to-xml crud


    【解决方案1】:

    我不明白这个“而且 ContractInfo 可能有你的可重复子节点”

    如果你想用 xml 进行 CRUD 操作,

    using System.Xml.Linq; //Name space 
    
    public class ContractInfo // Class for opertion - type
    {
      public int PledgeType {get; set;}
      public int ContractType {get; set;}
      public int ContractNum {get; set;}
    }
    
    public class Operations
    {
      public void insertContractInfo(ContractInfo obj)
      {
         XDocument contInfo = XDocument.load(HttpContext.Current.Server.MapPath("~/XMLFiles/contractInfo.xml"));
    
    
         contInfo.Root.Add(new XElement("ContractInfo",
               new XElement("PledgeType", obj.PledgeType),
               new XElement("ContractType", obj.ContractType),
               new XElement("ContractNum", obj.ContractNum)
               ));
        contInfo.Save(HttpContext.Current.Server.MapPath("~/XMLFiles/Appointments.xml"));
        string status = "success";
      }
    }
    

    这只是帮助您工作的一段示例代码。这行得通。对于编辑和更新,您可能需要使用 Element("Node name").Value

    【讨论】:

    • 30123AccNUm1200 AccNUm2200 如何使用(CRUD 操作) ContractInfo 的子节点“Accounts”?
    • 你不能处理任何随机的 xml,如果你有一个定义的模型很容易。否则你需要更多的条件
    • 我有一个巨大的 xml 文档,里面有 nodelist 层。我想使用选定的“ContractInfo[contractIndex]/Accounts/Account[AccIndex]”。我该怎么做?我必须使用子节点。
    • 可以说没有问题。如果您已经定义了您的 xml 模型,那么它可能很容易做到。为一个节点提供完整的 xml。它可能是可选的,但变化不大
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多