WCF 服务中的 LINQ to XML

发布于 2024-09-14 00:28:52 字数 885 浏览 2 评论 0原文

我正在使用 VS 2008。我必须创建一个 wcf 服务,并且在该服务内我必须使用 LINQ to XML 对 xml 文件执行一些基本操作。我必须在 IIS 7.0 中托管该服务并使用它。

作为 LINQ to XML 的一部分,我必须执行以下任务:

  1. 找到所有查询中答案不修改的
  2. 问题和答案的所有问题 id="Enquiry2"

下面粘贴的是 XML 文件

<b:ServeySet xmlns:b="b:urn:schemas-microsoft-com">
  <b:Enquires>
    <b:Enquiry id="Enquiry1">
      <b:Question id="1" question="is the summer hot in LA?" answer="yes"/>
      <b:Question id="3" question="is this hard exercise?" answer="no"/>
      <b:Question id="2" question="is this awesome?" answer="yes"/>
    </b:Enquiry >
    <b:Enquiry id="Enquiry2">
      <b:Question id="1" question="what is SFO?" answer="City"/>
      <b:Question id="2" question="is this hard exercise?" answer="no"/>
    </b:Enquiry >
  </b:Enquires>
</b:ServeySet>

I am using VS 2008. I have to create a wcf service and inside the service i have to do some basic operations on a xml file using LINQ to XML. I have to host the service in IIS 7.0 and consume it.

As part of the LINQ to XML I have to do the following tasks :

  1. find all the questions in all the Enquiry that answers are no
  2. modify the question and answer of Enquiry id="Enquiry2"

Pasted below is the XML file

<b:ServeySet xmlns:b="b:urn:schemas-microsoft-com">
  <b:Enquires>
    <b:Enquiry id="Enquiry1">
      <b:Question id="1" question="is the summer hot in LA?" answer="yes"/>
      <b:Question id="3" question="is this hard exercise?" answer="no"/>
      <b:Question id="2" question="is this awesome?" answer="yes"/>
    </b:Enquiry >
    <b:Enquiry id="Enquiry2">
      <b:Question id="1" question="what is SFO?" answer="City"/>
      <b:Question id="2" question="is this hard exercise?" answer="no"/>
    </b:Enquiry >
  </b:Enquires>
</b:ServeySet>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2024-09-21 00:28:52

尝试这样的事情:

static void Main(string[] args)
{
    // create the XDocument and load the file from disk
    XDocument doc = XDocument.Load("youroriginalfile.xml");

    // define the XML namespace used
    XNamespace b = "b:urn:schemas-microsoft-com:billing-data";

    // get all the questions where the answer is "no"
    var questionsWithAnswerNo = doc.Descendants(b + "Question").Where(x => x.Attribute("answer").Value == "no");

    // loop over these questions and do something.....
    foreach (XElement question in questionsWithAnswerNo)
    {
        // do something
    }

    // find questions for test with "test2" id
    var test2 = doc.Descendants(b + "Test").Where(x => x.Attribute("id").Value == "test2").FirstOrDefault();

    if(test2 != null)
    {
        // loop over questions, prepend a "TEST2: " to both question and
        // answer attribute of each of the questions
        foreach (XElement q2 in test2.Descendants(b + "Question"))
        {
            a2.Attribute("question").Value = "Test2: " + a2.Attribute("question").Value;
            a2.Attribute("answer").Value = "Test2: " + a2.Attribute("answer").Value;
        }

        // save the modified file
        doc.Save("newfile.xml");
    }

Try something like this:

static void Main(string[] args)
{
    // create the XDocument and load the file from disk
    XDocument doc = XDocument.Load("youroriginalfile.xml");

    // define the XML namespace used
    XNamespace b = "b:urn:schemas-microsoft-com:billing-data";

    // get all the questions where the answer is "no"
    var questionsWithAnswerNo = doc.Descendants(b + "Question").Where(x => x.Attribute("answer").Value == "no");

    // loop over these questions and do something.....
    foreach (XElement question in questionsWithAnswerNo)
    {
        // do something
    }

    // find questions for test with "test2" id
    var test2 = doc.Descendants(b + "Test").Where(x => x.Attribute("id").Value == "test2").FirstOrDefault();

    if(test2 != null)
    {
        // loop over questions, prepend a "TEST2: " to both question and
        // answer attribute of each of the questions
        foreach (XElement q2 in test2.Descendants(b + "Question"))
        {
            a2.Attribute("question").Value = "Test2: " + a2.Attribute("question").Value;
            a2.Attribute("answer").Value = "Test2: " + a2.Attribute("answer").Value;
        }

        // save the modified file
        doc.Save("newfile.xml");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文