标签: linq-to-xml

使用List.Add()方法将我的代码从foreach循环中分离出来

因此,从本质上讲,我运行到哪里,到了"的号召时,一个有趣的问题CreateXML()是由下面的代码"功能,旨在创建一个的XElement,但后来,当我尝试将其添加到xeleents集合,而不是继续调用" CreateXML()"发起的foreach循环,foreach循环被打破,并且调用" WriteXML()".此外,虽然创建并填充了XElement,但它不会添加到List中.[澄清一下,我所指的foreach循环以" ParseDoc()"方法生活]

    private List<XElement> _xelemlist;
    private void WriteXml()
    {
        XElement head = new XElement("header", new XAttribute("headerattributename", "attribute"));
        foreach (XElement xelem in _xelemlist)
        {
            head.Add(xelem);
        }
        XDocument doc = new XDocument();
        doc.Add(head);

    }
    private void CreateXML(string attname, string att)
    {
        XElement xelem = new XElement("name", new XElement("child", new XAttribute(attname, att), segment));
        _xelemlist.Add(xelem);
    }
    private void ExtractSegment(HtmlNode node)
    {
        HtmlAttribute[] segatts = node.Attributes.ToArray();
        string attname = segatts[0].Value.ToString();
        string att = node.InnerText.ToString();
        CreateXML(attname, att);
    }
    private …
Run Code Online (Sandbox Code Playgroud)

c# foreach list linq-to-xml

0
推荐指数
1
解决办法
325
查看次数

获得单个子元素的最佳方法

我们有C#代码来处理我们创建的各种XML文档.通常我们需要获得一个已知的子元素(它可能是唯一的孩子或可能有其他兄弟姐妹).我有一个给父母的函数,子名称将返回子元素:

public static XmlElement GetChildElement(XmlElement parentElement, string childName) 
{
    return parentElement.GetElementsByTagName(childName).Cast<XmlElement>().FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但有一天我想知道是否可以使用XPath或LINQ to XML更清晰,更容易.我发现的大多数XPath示例似乎都想知道文档的整个结构,我想要一个只知道父和子的泛型函数.Linq to XML似乎更有前途,但我没有找到一个匹配我正在寻找的例子.

c# xml xpath linq-to-xml

0
推荐指数
1
解决办法
120
查看次数

为什么IEnumerable中没有赋值?

我解析一些数据

var result = xml.Descendants("record").Select(x => new F130Account
    {
        Account = x.Descendants("Account").First().Value,
    });
Run Code Online (Sandbox Code Playgroud)

然后我尝试更新一些

foreach (var item in result)
    item.Quantity = 1;
Run Code Online (Sandbox Code Playgroud)

在此之后,我result.Sum(a => a.Quantity)有零...为什么?

c# linq-to-xml

0
推荐指数
1
解决办法
83
查看次数

无效的高代理字符(0xDEE1).高代理字符必须具有范围(0xD800 - 0xDBFF)中的值

我怎么能修复这个错误,我cardnumber只尝试加密,我在创建xml文件时没有任何问题,当我已经添加了expdate.它会提示错误,

到目前为止,这是我的代码:

    cardnumber = cs_aes.Encrypt(cardnumber);
expdate = cs_aes.Encrypt(expdate);

XElement xml_request = new XElement("Request",
                new XElement("Transaction",
                    new XElement("CardNumber", cardnumber),
                    new XElement("CardMember", cardmember),
                    new XElement("ExpDate", expdate),
                    new XElement("Amount", amount),
                    new XElement("Invoice", invoice),
                    new XElement("Zip", zip),
                    new XElement("Street", street),
                    new XElement("Security", security),
                    ));

                    xml_request.Save(path here);
Run Code Online (Sandbox Code Playgroud)

加密/解密代码

c# aes linq-to-xml

0
推荐指数
1
解决办法
2379
查看次数

使用Xdocument将xml文档的一部分复制到另一个文档

我有一个xml文档,大致如下

<Gov>
    <Head>
        <address></address>
        <address></address>
    </Head>
    <Body>
        <line1></line1>
        <line1></line1>
    </Body>
<Gov>
Run Code Online (Sandbox Code Playgroud)

我需要将正文中的所有内容(包括)复制到新的XDocument中.什么是最好的方法

c# xml linq-to-xml

0
推荐指数
1
解决办法
1万
查看次数

从Dictionary生成XML

我有一个带键值对的字典.我想用LINQ将它写入XML.

我能够使用LINQ创建XML文档,但不知道如何从字典中读取值并写入XML.

以下是使用硬编码值生成XML的示例,我想准备字典而不是硬编码值

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "true"),
    new XElement("countrylist",
        new XElement("country",
            new XAttribute("id", "EMP001"),
            new XAttribute("name", "EMP001")
        ),
        new XElement("country",
            new XAttribute("id", "EMP001"),
            new XAttribute("name", "EMP001")
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

c# xml dictionary linq-to-xml

0
推荐指数
1
解决办法
3220
查看次数

在XDocument中向元素添加元素

我一直在编写一个使用XML的数据库程序.每当程序引导并且在指定的路径中找不到XML文件时,它就会生成:

<!--  Studnet Database  -->
<schoolDB>
    <Grades>
        <Grade10/>
        <Grade11/>
        <Grade12/>
    </Grades>
    <Employees/>
</schoolDB>
Run Code Online (Sandbox Code Playgroud)

我希望程序在元素中添加一个element被调用者(提示用户输入10到12之间的等级,然后将其解析为Grade10 - > Grade12).我写了这个: studentGrade*

    XDocument doc = XDocument.Load(prog.dbFile);
    doc.Element(toWriteGrade).Add(new XElement("student",
            new XElement("name", name),
            new XElement("age", age)));
    doc.Save(prog.dbFile);
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它给了我一个错误:

StudentClone1.exe中出现未处理的"System.NullReferenceException"类型异常.附加信息:对象引用未设置为对象的实例.

这有什么不对?

c# xml linq linq-to-xml

0
推荐指数
1
解决办法
7626
查看次数

根据条件LINQ TO XML C#选择元素

我使用LINQ TO XML查询XML.我想基于一些条件检查查询记录这里是我的XML下面:XML将有多个订单,每个订单将在我想要选择的顺序,其中param节点属性存储值不为null,如果存在应该是1并且还订购纸箱信息......

    <orders>
        <order>
            <criteria>
                <param name="location">123</param>
                <param name="name">Chicago</param>
                <param name="Store">1</param>
                <param name="Account Number">1212121212</param>
            </criteria>
            <items>
                <item> </item>
                <item> </item>           
            </items>
            <cartons>
                <carton>
                     <size> </size>
                     <weight></weight>
                </carton>
            </cartons>
        </order>
    <Order>
    </Order>
    </orders>
Run Code Online (Sandbox Code Playgroud)

我能做到这一点:

var result = from item in doc.Descendants("order")
                where item.Element("criteria").Element("param").Attribute("name").Value.Contains("store") == true
                && item.Element("cartons") != null
                select item;"
Run Code Online (Sandbox Code Playgroud)

如果我的store(param)属性在过滤器节点中是第一个但是如果我将商店(param)移动到其他位置而不是首先它不过滤它,则上面工作正常

c# linq-to-xml

0
推荐指数
1
解决办法
2199
查看次数

如何使用C#从ALL XML Node值中删除所有尾随空格?

我正在尝试编写一个通用的C#.NET代码来检查XML文件中的每个XML节点的值,以便跟踪空格并删除它们.我尝试了PreserveWhiteSpace.但那没用.请指教.

<Sample>
<Item>
<Value>BatchID             </Value>
<Details>RecipeID              </Details>
</Item>
<Summary>Test data</Summary>
</Sample>
Run Code Online (Sandbox Code Playgroud)

c# xml linq-to-xml

0
推荐指数
1
解决办法
125
查看次数

C#中的后代没有给出正确的价值

我是非常新的XML使用C#我有一个XML我需要通过父亲中的特定子项我需要获取id和调用变量变量我这样做但每次都没有通过循环

我需要通过xml的所有父项,直到我得到我想要的树吗?

xml

<message xmlns="jabber:client" to="1072@finesse1.dcloud.cisco.com" id="/finesse/api/User/1072/Dialogs__1072@finesse1.dcloud.cisco.com__104Y2" from="pubsub.finesse1.dcloud.cisco.com">
<event xmlns="http://jabber.org/protocol/pubsub#event">
 <items node="/finesse/api/User/1072/Dialogs">
  <item id="460c2d27-c914-4c24-a95f-edf9f8df45c21535">
    <notification xmlns="http://jabber.org/protocol/pubsub">
      <Update>
        <data>
          <dialogs>
            <Dialog>
              <associatedDialogUri></associatedDialogUri>
              <fromAddress>1071</fromAddress>
              <id>18639330</id>
              <mediaProperties>
                <DNIS>1072</DNIS>
                <callType>AGENT_INSIDE</callType>
                <dialedNumber>1072</dialedNumber>
                <outboundClassification></outboundClassification>
                <callvariables>
                  <CallVariable>
                    <name>callVariable1</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable2</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable3</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable4</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable5</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable6</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable7</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable8</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable9</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable10</name>
                    <value></value>
                  </CallVariable>
                </callvariables>
              </mediaProperties>
              <mediaType>Voice</mediaType>
              <participants>
                <Participant>
                  <actions>
                    <action>ANSWER</action>
                  </actions>
                  <mediaAddress>1072</mediaAddress>
                  <mediaAddressType>AGENT_DEVICE</mediaAddressType>
                  <startTime>2017-09-15T19:23:36.872Z</startTime> …
Run Code Online (Sandbox Code Playgroud)

c# xml linq-to-xml

0
推荐指数
1
解决办法
350
查看次数

标签 统计

c# ×10

linq-to-xml ×10

xml ×6

aes ×1

dictionary ×1

foreach ×1

linq ×1

list ×1

xpath ×1