每次单击鼠标时将数据添加到 xml 文件

发布于 2024-11-18 09:34:25 字数 5695 浏览 1 评论 0原文

每次鼠标事件发生时,我都必须将数据添加到 xml 文件!这是我的代码,您看一下后我会更好地向您解释。

 private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {


            if (radioButton1.Checked==true)
            {
                if (a > 3 && a < 11)
                {

                    rect.Width = 0;
                    rect.Height = 0;
                    pictureBox1.Invalidate();


                 raghu =   img.imageToByteArray(pictureBox1.Image);


                    int radius = 10; //Set the number of pixel you want to use here
                    //Calculate the numbers based on radius
                    int x0 = Math.Max(e.X - (radius / 2), 0),
                        y0 = Math.Max(e.Y - (radius / 2), 0),
                        x1 = Math.Min(e.X + (radius / 2), pictureBox1.Width),
                        y1 = Math.Min(e.Y + (radius / 2), pictureBox1.Height);
                    Bitmap bm = pictureBox1.Image as Bitmap; //Get the bitmap (assuming it is stored that way)
                    for (int ix = x0; ix < x1; ix++)
                    {
                        for (int iy = y0; iy < y1; iy++)
                        {
                            bm.SetPixel(ix, iy, Color.Black); //Change the pixel color, maybe should be relative to bitmap
                        }
                    }
                    pictureBox1.Refresh(); //Force refresh

                    //calculation part.
                    float u = l[p + 1] - l[p];
                    float v = m[p + 1] - m[p];
                    float w = (e.Y - m[p]) / v;//subtract from latitude
                    float z = (e.X - l[p]) / u;//add to longitude.
                    float latmin = h[p] - w;
                    float longmin = j[p] + z;

                    A1 = e.X - c[p];
                    A2 = e.Y - d[p];
                    B1 = c[p + 1] - c[p];
                    B2 = d[p + 1] - d[p];
                    A = Math.Sqrt(Math.Pow(A1, 2) + Math.Pow(A2, 2));
                    B = Math.Sqrt(Math.Pow(B1, 2) + Math.Pow(B2, 2));
                    dotproduct = A1 * B1 + A2 * B2;
                    theta = (180 / Math.PI) * Math.Acos(dotproduct / (A * B));
                    if (e.X < c[p])
                    {
                        theta1 = 360 - theta;
                    }
                    else
                    {
                        theta1 = theta;
                    }
                    textBox2.Text = string.Format("Latitude:{0}°{1}'{2}''\n Longitude:{3}°{4}'{5}'' \n ICAO LOC: {6} {7} \n Distance: {8:0.0} Nm", g[p] + (int)latmin / 60, (int)latmin % 60,(int) (((h[p]-w) % 60 - (int)(latmin % 60)) * 60), i[p] + (int)longmin / 60, (int)longmin % 60, (int)((longmin % 60 - (int)(longmin % 60)) * 60), textBox1.Text, Math.Abs((int)theta1), Math.Sqrt(Math.Pow((A1/u)* 60, 2) + Math.Pow((A2/v) * 60, 2))); 

现在有很多计算部分是不必要的。但是我要在 textBox2 中打印的数据(最后一行代码)很重要。每次我打印的所有数据都应该添加到 Xml 文件中单击图片框上的鼠标...

所以我尝试通过添加这种方式使用 xdocument 来实现..

             XDocument xdoc = XDocument.Load(string.Format("{0}.xml", textBox3.Text));
                    XElement ParentNode= xdoc.Root.Element(string.Format("{0}",radioButton1.Text));
                    XElement node= new XElement("WayPoints",new XElement("LatitudeDegrees",g[p] + (int)latmin / 60 ,
                                     new XAttribute("Minutes",(int)latmin % 60),
                                         new XAttribute("Seconds",(int)(((h[p] - w) % 60 - (int)(latmin % 60)) * 60)),
                                            new XElement("LongitudeDegrees",i[p] + (int)longmin / 60,
                                                new XAttribute("Minutes",(int)longmin % 60),
                                                  new XAttribute("seconds",(int)((longmin % 60 - (int)(longmin % 60)) * 60)),
                                                     new XElement ("IcaoLocator",textBox1.Text,
                                                         new XAttribute("angle",Math.Abs((int)theta1)),
                                                         new XElement("DistanceInNauticalMiles",Math.Sqrt(Math.Pow((A1 / u) * 60, 2) + Math.Pow((A2 / v) * 60, 2))

                )))));
                    ParentNode.Add(node);
                    xdoc.Save(string.Format("{0}.xml", textBox3.Text));

但问题是它说根元素丢失。

实际上我在 Xdocument 中加载的文件是,我

  using (XmlWriter writer = XmlWriter.Create(string.Format("{0}.xml", textBox3.Text)))
            { 
            }

之前已经在这里创建了它,如下所示(因为我担心如果在 Picturebox_Mouseclick 中创建它,它会被覆盖)

 private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {

            if (a > 1 && a <= 3)
            {
                c[f] = e.X;
                d[f] = e.Y;
                a++;
                f++;
            }
          using (XmlWriter writer = XmlWriter.Create(string.Format("{0}.xml", textBox3.Text)))
            { 
            }
    }        

如果您不理解计算,请不要打扰,它特定于我的需求我只需要将数据打印到 xml 文件..请任何人帮助我..

这里有一个航点示例,

<?xml version="1.0" encoding="UTF-8"?>
<WayPoints>
  <Latitude>
    <Degrees>48</Degrees>
    <Minutes>31.7363644</Minutes>
  </Latitude>
  <Longitude>
    <Degrees>11</Degrees>
    <Minutes>57.53425</Minutes>
  </Longitude>
  <IcaoLocator>
    <Type>EDML</Type>
    <Angle>288</Angle>
    <DistanceInPixels>346</DistanceInPixels>
  </IcaoLocator>
</WayPoints>

但它只包含一个航点..我需要向这个 XML 文件添加很多航点。

i have to add data to an xml file everytime the mouse event happens! here is my code i will explain you better after you having a glance at it.

 private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {


            if (radioButton1.Checked==true)
            {
                if (a > 3 && a < 11)
                {

                    rect.Width = 0;
                    rect.Height = 0;
                    pictureBox1.Invalidate();


                 raghu =   img.imageToByteArray(pictureBox1.Image);


                    int radius = 10; //Set the number of pixel you want to use here
                    //Calculate the numbers based on radius
                    int x0 = Math.Max(e.X - (radius / 2), 0),
                        y0 = Math.Max(e.Y - (radius / 2), 0),
                        x1 = Math.Min(e.X + (radius / 2), pictureBox1.Width),
                        y1 = Math.Min(e.Y + (radius / 2), pictureBox1.Height);
                    Bitmap bm = pictureBox1.Image as Bitmap; //Get the bitmap (assuming it is stored that way)
                    for (int ix = x0; ix < x1; ix++)
                    {
                        for (int iy = y0; iy < y1; iy++)
                        {
                            bm.SetPixel(ix, iy, Color.Black); //Change the pixel color, maybe should be relative to bitmap
                        }
                    }
                    pictureBox1.Refresh(); //Force refresh

                    //calculation part.
                    float u = l[p + 1] - l[p];
                    float v = m[p + 1] - m[p];
                    float w = (e.Y - m[p]) / v;//subtract from latitude
                    float z = (e.X - l[p]) / u;//add to longitude.
                    float latmin = h[p] - w;
                    float longmin = j[p] + z;

                    A1 = e.X - c[p];
                    A2 = e.Y - d[p];
                    B1 = c[p + 1] - c[p];
                    B2 = d[p + 1] - d[p];
                    A = Math.Sqrt(Math.Pow(A1, 2) + Math.Pow(A2, 2));
                    B = Math.Sqrt(Math.Pow(B1, 2) + Math.Pow(B2, 2));
                    dotproduct = A1 * B1 + A2 * B2;
                    theta = (180 / Math.PI) * Math.Acos(dotproduct / (A * B));
                    if (e.X < c[p])
                    {
                        theta1 = 360 - theta;
                    }
                    else
                    {
                        theta1 = theta;
                    }
                    textBox2.Text = string.Format("Latitude:{0}°{1}'{2}''\n Longitude:{3}°{4}'{5}'' \n ICAO LOC: {6} {7} \n Distance: {8:0.0} Nm", g[p] + (int)latmin / 60, (int)latmin % 60,(int) (((h[p]-w) % 60 - (int)(latmin % 60)) * 60), i[p] + (int)longmin / 60, (int)longmin % 60, (int)((longmin % 60 - (int)(longmin % 60)) * 60), textBox1.Text, Math.Abs((int)theta1), Math.Sqrt(Math.Pow((A1/u)* 60, 2) + Math.Pow((A2/v) * 60, 2))); 

There is a lot of calculation part which is not necessary now.But the data i am going to print in the textBox2(last line of code) is important.all that data that is printed there should be added to an Xml file each time i click the mouse on the picturebox...

so I tried to implement by using xdocument by adding this way..

             XDocument xdoc = XDocument.Load(string.Format("{0}.xml", textBox3.Text));
                    XElement ParentNode= xdoc.Root.Element(string.Format("{0}",radioButton1.Text));
                    XElement node= new XElement("WayPoints",new XElement("LatitudeDegrees",g[p] + (int)latmin / 60 ,
                                     new XAttribute("Minutes",(int)latmin % 60),
                                         new XAttribute("Seconds",(int)(((h[p] - w) % 60 - (int)(latmin % 60)) * 60)),
                                            new XElement("LongitudeDegrees",i[p] + (int)longmin / 60,
                                                new XAttribute("Minutes",(int)longmin % 60),
                                                  new XAttribute("seconds",(int)((longmin % 60 - (int)(longmin % 60)) * 60)),
                                                     new XElement ("IcaoLocator",textBox1.Text,
                                                         new XAttribute("angle",Math.Abs((int)theta1)),
                                                         new XElement("DistanceInNauticalMiles",Math.Sqrt(Math.Pow((A1 / u) * 60, 2) + Math.Pow((A2 / v) * 60, 2))

                )))));
                    ParentNode.Add(node);
                    xdoc.Save(string.Format("{0}.xml", textBox3.Text));

But the problem is it is saying that the root element is Missing.

Actually the file I am loading in the Xdocument is ,I already created it

  using (XmlWriter writer = XmlWriter.Create(string.Format("{0}.xml", textBox3.Text)))
            { 
            }

previously here like this(because I fear that if create it is created in Picturebox_Mouseclick it gets over written)

 private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {

            if (a > 1 && a <= 3)
            {
                c[f] = e.X;
                d[f] = e.Y;
                a++;
                f++;
            }
          using (XmlWriter writer = XmlWriter.Create(string.Format("{0}.xml", textBox3.Text)))
            { 
            }
    }        

Dont bother if you dont understand the calculation ,its specific to my needs I only need to print the data to xml file..please any one kindly help me..

one example of way point is here

<?xml version="1.0" encoding="UTF-8"?>
<WayPoints>
  <Latitude>
    <Degrees>48</Degrees>
    <Minutes>31.7363644</Minutes>
  </Latitude>
  <Longitude>
    <Degrees>11</Degrees>
    <Minutes>57.53425</Minutes>
  </Longitude>
  <IcaoLocator>
    <Type>EDML</Type>
    <Angle>288</Angle>
    <DistanceInPixels>346</DistanceInPixels>
  </IcaoLocator>
</WayPoints>

but it contains only one way point ..I ned to add a lot of waypoints to this XML file.

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

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

发布评论

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

评论(2

听风念你 2024-11-25 09:34:25

这里的问题似乎是事实上(正如例外情况)xml 没有根元素。通过将 xml 的布局更改为如下所示,可以相当简单地解决此问题:

<?xml version="1.0" encoding="UTF-8"?>
<WayPoints>
   <WayPoint>
      <Latitude>
        <Degrees>48</Degrees>
        <Minutes>31.7363644</Minutes>
      </Latitude>
      <Longitude>
        <Degrees>11</Degrees>
        <Minutes>57.53425</Minutes>
      </Longitude>
      <IcaoLocator>
        <Type>EDML</Type>
        <Angle>288</Angle>
        <DistanceInPixels>346</DistanceInPixels>
      </IcaoLocator>
   </WayPoint>
   <WayPoint> <!-- New waypoint here, not at the root -->
      <Latitude>
        <Degrees>48</Degrees>
        <Minutes>31.7363644</Minutes>
      </Latitude>
      <Longitude>
        <Degrees>11</Degrees>
        <Minutes>57.53425</Minutes>
      </Longitude>
      <IcaoLocator>
        <Type>EDML</Type>
        <Angle>288</Angle>
        <DistanceInPixels>346</DistanceInPixels>
      </IcaoLocator>
   </WayPoint>
</WayPoints>

然后您应该能够执行以下操作:

var doc = XDocument.Load("PATH_TO_FILE.xml");
doc.Element("WayPoints").Add(new XElement("WayPoint", 
    new XElement("Latitude",
        new XElement("Degrees", 48),
        new XElement("Minutes", 31.7363644),
    ),
    new XElement("Longitude",
        new XElement("Degrees", 48),
        new XElement("Minutes", 31.7363644),
    ),
    new XElement("IcaoLocator",
        new XElement("Type", "EDML"),
        new XElement("Angle", 288),
        new XElement("DistanceInPixels", 346)
    )
);
doc.Save("PATH_TO_FILE.xml");

未测试(此计算机上没有 Visual Studio)。

The problem here seems to be that there is in fact (as the exception states) no root element for the xml. This can be fixed fairly simple by changing the layout of the xml to something like this:

<?xml version="1.0" encoding="UTF-8"?>
<WayPoints>
   <WayPoint>
      <Latitude>
        <Degrees>48</Degrees>
        <Minutes>31.7363644</Minutes>
      </Latitude>
      <Longitude>
        <Degrees>11</Degrees>
        <Minutes>57.53425</Minutes>
      </Longitude>
      <IcaoLocator>
        <Type>EDML</Type>
        <Angle>288</Angle>
        <DistanceInPixels>346</DistanceInPixels>
      </IcaoLocator>
   </WayPoint>
   <WayPoint> <!-- New waypoint here, not at the root -->
      <Latitude>
        <Degrees>48</Degrees>
        <Minutes>31.7363644</Minutes>
      </Latitude>
      <Longitude>
        <Degrees>11</Degrees>
        <Minutes>57.53425</Minutes>
      </Longitude>
      <IcaoLocator>
        <Type>EDML</Type>
        <Angle>288</Angle>
        <DistanceInPixels>346</DistanceInPixels>
      </IcaoLocator>
   </WayPoint>
</WayPoints>

Then you should be able to do this:

var doc = XDocument.Load("PATH_TO_FILE.xml");
doc.Element("WayPoints").Add(new XElement("WayPoint", 
    new XElement("Latitude",
        new XElement("Degrees", 48),
        new XElement("Minutes", 31.7363644),
    ),
    new XElement("Longitude",
        new XElement("Degrees", 48),
        new XElement("Minutes", 31.7363644),
    ),
    new XElement("IcaoLocator",
        new XElement("Type", "EDML"),
        new XElement("Angle", 288),
        new XElement("DistanceInPixels", 346)
    )
);
doc.Save("PATH_TO_FILE.xml");

Not tested (don't have visual studio on this computer).

凉城凉梦凉人心 2024-11-25 09:34:25

当您使用 xml 编写器创建 xml 文件时,您不会在其中放入任何元素,因此当 XDocument 加载它时,它找不到根元素并抛出您收到的异常。

如果文件不存在,我建议跳过 xmlwriter 并将 XDocument 初始化为新的 XDocument:

XDocument xdoc;
if( File.Exist( string.Format("{0}.xml", textBox3.Text ) ) )
  xdoc = XDocument.Load( string.Format( "{0}.xml", textBox3.Text ) );
else
  xdoc = new XDocument(...);

When you use the xml writer to create the xml file, you dont put any elements in it, so when XDocument goes to load it, it doesnt find a root element and throws the exception you're getting.

i suggest skipping the xmlwriter and initializing the XDocument to a new XDocument if the file doesnt exist:

XDocument xdoc;
if( File.Exist( string.Format("{0}.xml", textBox3.Text ) ) )
  xdoc = XDocument.Load( string.Format( "{0}.xml", textBox3.Text ) );
else
  xdoc = new XDocument(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文