1

I am new in Linq to xml. My xml string is as follows :

 <root>
    <add>
       <item id="jqg1" moduleid="F0736590-84A2-4795-BC5D-0056606F2446" view="1" add="1" edit="0" del="0" /> 
    </add>
   <update>
        <item id="jqg3" moduleid="3C414435-DBBE-4B4E-A790-14B67F050EB0" view="1" add="1" edit="1" del="0" /> 
    </update>
  </root>

coming from jquery to C# which is placed inside texbox(textbox Visible = false).

NOTE : element could be more than one in every and element.

Code to read xml string in C# on button click eevnt.

string paramXML = txtXmlStr.Text;
var l_sID = "";
var l_sMODID = "";
var l_sVIEW = ""; 
var l_sADD = "";
var l_sEDIT = "";
var l_sDEL = "";

if (!string.Empty.Equals(paramXML))
{
    var l_oXDoc = XDocument.Parse(paramXML.ToLower());

    ==> var addParams = from addParam in l_oXDoc.Element("add").Elements("item")
                        select new
                        {
                            l_sID = addParam.Attribute("id").Value,
                            l_sMODID = addParam.Attribute("moduleId").Value,
                            l_sVIEW = addParam.Attribute("view").Value,
                            l_sADD = addParam.Attribute("add").Value,
                            l_sEDIT = addParam.Attribute("edit").Value,
                            l_sDEL = addParam.Attribute("del").Value,
                        };

        foreach (var oParam in addParams)
        {
            string id = oParam.l_sID.Trim();
            string modid = oParam.l_sMODID.Trim();
            string view = oParam.l_sMODID.Trim();
            string add = oParam.l_sMODID.Trim();
            string edit = oParam.l_sMODID.Trim();
            string del = oParam.l_sMODID.Trim();
        }

I am getting above error on arrow which I already specified in code. How should I resolve this error???

*NOTE:*My goal is to read <Add> element's <item> element data into one variable and I also want to do same for <update> element. Is that possible.... everyones suggestion, ideas and code is welcome....

4

2 回答 2

1

属性/元素名称区分大小写。

代替

l_sMODID = addParam.Attribute("moduleId").Value,

l_sMODID = addParam.Attribute("moduleid").Value,

那是因为输入 XML 中的属性是moduleid="F0736590-84A2-4795-BC5D-0056606F2446",而不是moduleId

工作代码:

var doc = XDocument.Load("Input.txt");

Func<XElement, IEnumerable<Item>> query = e => e.Elements("item")
                       .Select(x => new Item
                       {
                           Id = (string)x.Attribute("id"),
                           ModuleId = (string)x.Attribute("moduleid"),
                           View = (bool)x.Attribute("view"),
                           Add = (bool)x.Attribute("add"),
                           Edit = (bool)x.Attribute("edit"),
                           Delete = (bool)x.Attribute("del")
                       });

var addItems = query(doc.Root.Element("add")).ToList();
var updateItems = query(doc.Root.Element("update")).ToList();

Item类声明:

public class Item
{
    public string Id { get; set; }
    public string ModuleId { get; set; }
    public bool View { get; set; }
    public bool Add { get; set; }
    public bool Edit { get; set; }
    public bool Delete { get; set; }
}
于 2013-09-06T14:04:58.847 回答
1

您需要先获取根元素。改变:

var addParams = from addParam in l_oXDoc.Element("add").Elements("item")

var addParams = from addParam in 
l_oXDoc.Descendants("root").First().Element("add").Elements("item")
于 2013-09-06T15:55:22.513 回答